1.創(chuàng)建UIView
2.在UIView.h中創(chuàng)建兩個(gè)屬性
@property UILabel *aLabel;
@property UITextField *textField;
3.在UIView.m中重寫(xiě)初始化方法.創(chuàng)建新方法
//重寫(xiě)初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setLabel];
[self setTextField];
}
return self;
}
//模塊化創(chuàng)建label
//野指針 就是沒(méi)有指向確定空間的指針
- (void)setLabel {
_aLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
_aLabel.backgroundColor = [UIColor brownColor];
_aLabel.textColor = [UIColor cyanColor];
[self addSubview:_aLabel];
}
- (void)setTextField{
_textField = [[UITextField alloc]initWithFrame:CGRectMake(130, 10, 250, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;//默認(rèn)白色
_textField.clearButtonMode = UITextFieldViewModeAlways;
[self addSubview:_textField];
}
4.在RootViewController.m的viewDidLoad中調(diào)用LTView
LTView *aView = [[LTView alloc]initWithFrame:CGRectMake(10, 50, 390, 60)];
aView.backgroundColor = [UIColor yellowColor];
aView.aLabel.text = @"賬號(hào)";
aView.textField.placeholder = @"請(qǐng)輸入賬號(hào)";
[self.view addSubview:aView];