UI之instancetype和id的区别、Custom
2015-12-11 18:02
369 查看
相同点:都是方法的返回值,可以代表任意类型
不同点:
1.instancetype可以返回和方法所在类相同类型的对象(instancetype会告诉编译器当前类的类型(名字));
id只是返回未知类型的对象。
2.instancetype只能作为方法的返回值,id还可以作为参数(for id obj inXXX)
3.對于init方法来说,id和instancetype其实没有区别,因为编译器会把id优化成instancetype
4.好处:确定对象类型帮助编译器更好的定位代码问题
由于有些像注册信息的平台,会需要有很多的label和textField,这样就会很浪费开发人员的时间和精力去编写一些相同的东西,所以我们可以采用一种简单的方法来实现这一功能,重新创建一个LTView继承于UIView且需要遵循UITextFieldDelegate协议
在LTView.h中
#import <UIKit/UIKit.h>
//引入头文件
#import "MainViewController.h"
@interface LTView : UIView<UITextFieldDelegate>
//@property(nonatomic,retain)id init;
//把label定义成属性,在LTView外部使用设置label的文字或者其他属性
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UITextField *textField;
@end
接下来在LTView.m中,
#import "LTView.h"
#define kWidth self.frame.size.width
#define kHeight self.frame.size.height
@implementation LTView
@synthesize label = _label;
@synthesize textField = _textField;
//重写init方法在初始化LTView时会执行子类的init方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//自定义方法insert code···
// 调用函数
[self loadingCustomView];
}
return self;
}
//属性生成的实例变量需要在dealloc中释放
- (void)dealloc{
[self.label release];
[self.textField release];
[super dealloc];
}
//函数
- (void)loadingCustomView{
// 创建标签
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth * 0.25, kHeight)];
self.label.backgroundColor = [UIColor cyanColor];
self.label.text = @"用户名";
self.label.textAlignment = NSTextAlignmentCenter;
// 创建文本框
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(kWidth/4, 0, kWidth*0.75, kHeight)];
self.textField.backgroundColor = [UIColor lightGrayColor];
self.textField.placeholder = @"请输入手机号/邮箱";
self.textField.textAlignment = NSTextAlignmentCenter;
self.textField.delegate = self;
// self本身就代指LTView
[self addSubview:self.label];
[self addSubview:self.textField];
[self.label release];
[self.textField release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
完成这些之后,在MainViewController.m中我们就可以根据需求写入内容
self.view.backgroundColor = [UIColor yellowColor];
LTView *ltView1 = [[LTView alloc]initWithFrame:CGRectMake(40, 100, 300, 40)];
ltView1.label.text = @"用户名";
ltView1.textField.placeholder = @"请输入手机号/邮箱";
[self.view addSubview:ltView1];
[ltView1 release];
LTView *ltView2 = [[LTView alloc]initWithFrame:CGRectMake(40, 160, 300, 40)];
ltView2.label.text = @"密码";
ltView2.textField.placeholder = @"请输入密码";
[self.view addSubview:ltView2];
[ltView2 release];
LTView *ltView3 = [[LTView alloc]initWithFrame:CGRectMake(40, 220, 300, 40)];
ltView3.label.text = @"确认密码";
ltView3.textField.placeholder = @"请再次输入密码 ";
[self.view addSubview:ltView3];
[ltView3 release];
LTView *ltView4 = [[LTView alloc]initWithFrame:CGRectMake(40, 280, 300, 40)];
ltView4.label.text = @"手机号";
ltView4.textField.placeholder = @"请输入手机号";
[self.view addSubview:ltView4];
[ltView4 release];
OK,这样再加上两个按钮,我们就可以完成一个简单注册界面的编写,是不是减少了很多代码?有了这个方法,我们以后再编写这些界面就简单的多了。
不同点:
1.instancetype可以返回和方法所在类相同类型的对象(instancetype会告诉编译器当前类的类型(名字));
id只是返回未知类型的对象。
2.instancetype只能作为方法的返回值,id还可以作为参数(for id obj inXXX)
3.對于init方法来说,id和instancetype其实没有区别,因为编译器会把id优化成instancetype
4.好处:确定对象类型帮助编译器更好的定位代码问题
由于有些像注册信息的平台,会需要有很多的label和textField,这样就会很浪费开发人员的时间和精力去编写一些相同的东西,所以我们可以采用一种简单的方法来实现这一功能,重新创建一个LTView继承于UIView且需要遵循UITextFieldDelegate协议
在LTView.h中
#import <UIKit/UIKit.h>
//引入头文件
#import "MainViewController.h"
@interface LTView : UIView<UITextFieldDelegate>
//@property(nonatomic,retain)id init;
//把label定义成属性,在LTView外部使用设置label的文字或者其他属性
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UITextField *textField;
@end
接下来在LTView.m中,
#import "LTView.h"
#define kWidth self.frame.size.width
#define kHeight self.frame.size.height
@implementation LTView
@synthesize label = _label;
@synthesize textField = _textField;
//重写init方法在初始化LTView时会执行子类的init方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//自定义方法insert code···
// 调用函数
[self loadingCustomView];
}
return self;
}
//属性生成的实例变量需要在dealloc中释放
- (void)dealloc{
[self.label release];
[self.textField release];
[super dealloc];
}
//函数
- (void)loadingCustomView{
// 创建标签
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth * 0.25, kHeight)];
self.label.backgroundColor = [UIColor cyanColor];
self.label.text = @"用户名";
self.label.textAlignment = NSTextAlignmentCenter;
// 创建文本框
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(kWidth/4, 0, kWidth*0.75, kHeight)];
self.textField.backgroundColor = [UIColor lightGrayColor];
self.textField.placeholder = @"请输入手机号/邮箱";
self.textField.textAlignment = NSTextAlignmentCenter;
self.textField.delegate = self;
// self本身就代指LTView
[self addSubview:self.label];
[self addSubview:self.textField];
[self.label release];
[self.textField release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
完成这些之后,在MainViewController.m中我们就可以根据需求写入内容
self.view.backgroundColor = [UIColor yellowColor];
LTView *ltView1 = [[LTView alloc]initWithFrame:CGRectMake(40, 100, 300, 40)];
ltView1.label.text = @"用户名";
ltView1.textField.placeholder = @"请输入手机号/邮箱";
[self.view addSubview:ltView1];
[ltView1 release];
LTView *ltView2 = [[LTView alloc]initWithFrame:CGRectMake(40, 160, 300, 40)];
ltView2.label.text = @"密码";
ltView2.textField.placeholder = @"请输入密码";
[self.view addSubview:ltView2];
[ltView2 release];
LTView *ltView3 = [[LTView alloc]initWithFrame:CGRectMake(40, 220, 300, 40)];
ltView3.label.text = @"确认密码";
ltView3.textField.placeholder = @"请再次输入密码 ";
[self.view addSubview:ltView3];
[ltView3 release];
LTView *ltView4 = [[LTView alloc]initWithFrame:CGRectMake(40, 280, 300, 40)];
ltView4.label.text = @"手机号";
ltView4.textField.placeholder = @"请输入手机号";
[self.view addSubview:ltView4];
[ltView4 release];
OK,这样再加上两个按钮,我们就可以完成一个简单注册界面的编写,是不是减少了很多代码?有了这个方法,我们以后再编写这些界面就简单的多了。
相关文章推荐
- Windows Driver升级到VS2015+Win10 WDK后build的Driver在Win7无法加载
- 爬爬爬之路:UI(十八) 集合视图(UICollectionView)
- 【转】Cloudera Hue Issues
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 自己项目中做的UI布局
- 复习了一下NGUI
- UE4的NetWork简单原理
- 49 You enable block change tracking. You issue the following command: BACKUP INCREMENTAL LEVEL 0 DAT
- UICollectionView的用法小结
- UI之textField、Button的创建方法及各属性
- Java 中 StringBuilder 在高性能用法总结