使用UIButton时需要注意的是:

1.UIButton的创建有专门的类方法(buttonWithType:,UILabel没有);

2.UIButton常用的属性包括:frame、titile、tag等;

3.UIButton使用addTarget方法关联处理方法,一个UIButton可以有多个不同的按钮响应方法,多个UIButton也可以共享一个处理方法(用tag做区分);

// 创建文件按钮
- (void) createRectUI {
// 穿件圆角矩形按钮
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(100, 100, 100, 50);
[btn1 setTitle:@"按钮1" forState:UIControlStateNormal];
btn1.titleLabel.font = [UIFont systemFontOfSize:18];
[self.view addSubview:btn1]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(100, 200, 100, 50);
[btn2 setTitle:@"按钮2" forState:UIControlStateNormal];
btn2.titleLabel.font = [UIFont systemFontOfSize:18];
[self.view addSubview:btn2]; btn1.tag = 101;
[btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
btn2.tag = 102;
[btn2 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
} - (void) btnPressed : (UIButton*) btn {
NSInteger tag = btn.tag;
if(tag == 101){
NSLog(@"btn1 pressed!");
}else{
NSLog(@"btn2 pressed!");
}
}

  

04-30 19:09