按钮通常使用 Touch Up Inside 事件来体现,能够抓取用户用手指按下并在该按钮上松开发生的事件。
当检测到事件后,便可能触发相应视图控件中的操作(IBAction)
//创建一个按钮 初始化按钮的frame
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50,50,50,50)];
//创建一个圆角矩形的按钮
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// 设置背景色
button.backgroundColor =[UIColor whiteColor];
// 字体大小
button.titleLabel.font = [UIFont systemFontOfSize:14];
// 在view上的位置
button.frame =CGRectMake(20,20,20,20)
// 文字内容
[button setTitle:@“这是按钮哦” forState:UIControlStateNormal];
// 文字颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 图片
[button setImage:[UIImage imageNamed:[UIImage imageNamed:@“123.png”]] forState:UIControlStateNormal];
//监听按钮点击
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpIn