您的位置:首页 > 其它

自定义视图

2015-08-08 19:02 281 查看
实现一个功能,不是难事。写出高质量的代码却不是任何程序员都能做到的事。

高质量代码特点:可复用,可移植,精炼等

自定义视图:系统标准UI之外,⼰己组合而出的新的视图。

iOS提供了很多UI组件,借助它们,我们可以做各种程序。

尽管如此,实际开发中,我们还需自定义视图.积累自己的代码库.方便开发.自己封装的视图,能像系统UI控件一样,用于别的项目中,能大大降低开发成本,提高开发效率

自定义视图步骤

根据需求的不同,自定义视图继承的类也有所不同。一般自定义的视图会继承于UIView。以下是自定义视图的要点:

1、创建一个UIView⼦子类

2、在类的初始化方法中添加子视图

3、类的.h文件提供一些接口(方法),便于外界操作子视图

#import "AppDelegate.h"
#import "LTView.h"
// 可以在.m文件中引头文件,签协议
@interface AppDelegate ()<UIAlertViewDelegate>

@property(nonatomic, retain)UIAlertView *alertView;

@end

@implementation AppDelegate

- (void)dealloc
{
[self.window release];
[self.alertView release];
[super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self.window release];

// alert 提示 提醒 警告 注意
self.alertView.delegate = self;
self.alertView = [[UIAlertView alloc] initWithTitle:@"皇城PK" message:@"怒刷装备" delegate:self cancelButtonTitle:@"注册即送" otherButtonTitles:@"残忍拒绝", nil];
[self.alertView show];

// 让alertView中出现textField
//    self.alertView.alertViewStyle =  UIAlertViewStyleLoginAndPasswordInput; // Login and Password
self.alertView.alertViewStyle =     UIAlertViewStyleSecureTextInput;    // Password
//    self.alertView.alertViewStyle = UIAlertViewStylePlainTextInput;     // Space

LTView *view = [[LTView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
[self.window addSubview:view];
[view release];
return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"我打~");
// 先找到alertView中的textField
UITextField *first = [self.alertView textFieldAtIndex:0];
NSLog(@"%@", first.text);
}

@end


#import <UIKit/UIKit.h>

@interface LTView : UIView<UITextFieldDelegate>

// 因为要在类的外面获取输入框的内容并修改Label的标题,所以把这两部分属性写在.h文件中
@property(nonatomic, retain)UILabel *MyLaber;

@property(nonatomic, retain)UITextField *MyTextField;

@end


#import "LTView.h"

@implementation LTView

// 重写默认的初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 模块化
[self createView1];
[self createView2];
}

return self;
}

- (void)createView1
{
// 创建两个子视图,一个是label,一个是textField
self.MyLaber = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 40)];
self.MyLaber.backgroundColor = [UIColor orangeColor];
self.MyTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 100, 180, 40)];
self.MyTextField.backgroundColor = [UIColor cyanColor];
self.MyTextField.delegate = self;
[self addSubview:self.MyLaber];
[self addSubview:self.MyTextField];
[self.MyLaber release];
[self.MyTextField release];
self.MyLaber.text = @"用户名";
self.MyTextField.placeholder = @"请输入您的用户名";
}

- (void)createView2
{
self.MyLaber = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 100, 40)];
self.MyLaber.backgroundColor = [UIColor yellowColor];
[self addSubview:self.MyLaber];
[self.MyLaber release];
self.MyTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 160, 180, 40)];
self.MyTextField.backgroundColor = [UIColor blueColor];
[self addSubview:self.MyTextField];
[self.MyTextField release];
self.MyLaber.text = @"密码";
self.MyTextField.placeholder = @"请输入您的密码";
}

- (void)dealloc
{
[self.MyTextField release];
[self.MyLaber release];
[super dealloc];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: