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(@"按钮被点击了");
}
本文介绍如何使用iOS中的UIButton组件,并为其添加点击事件监听方法。通过示例代码展示了如何创建一个按钮,设置其背景颜色,以及如何定义点击事件的响应。
578

被折叠的 条评论
为什么被折叠?



