UI之instancetype和id的区别、Custom

相同点:都是方法的返回值,可以代表任意类型

不同点:

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,这样再加上两个按钮,我们就可以完成一个简单注册界面的编写,是不是减少了很多代码?有了这个方法,我们以后再编写这些界面就简单的多了。


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值