addTarget可以为button添加监听方法:
示例代码为:
//实例化一个button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
//为button设置背景颜色
[button setBackgroundColor:[UIColor redColor]];
//添加监听方法
[button addTarget:self
action:@selector(clickButton)
forControlEvents:UIControlEventTouchUpInside];
//添加button
[self addSubview:button];
-(void)clickButton{
NSLog(@"按钮被点击了");
}