UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor grayColor];
button.frame = CGRectMake(10, 20, 80,80);
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1;
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.backgroundColor = [UIColor grayColor];
button1.frame = CGRectMake(10, 110, 80,80);
[button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button1.tag = 2;
[self.view addSubview:button1];
_label = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 200, 20)];
[self.view addSubview:_label];
}
-(void)buttonClicked:(UIButton *)btn{
_label.text = [NSString stringWithFormat:@"%zi",btn.tag];
NSLog(@"按钮%zi被点击",btn.tag);
}
UIButton的使用
原文:http://www.cnblogs.com/banchuangshuying/p/4985622.html