iOS入门之UI03 --UIButton
/**
*UIButton 按钮 触发点击事件
*继承自UIVIew,大小,边框,背景色设置相同
*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
/**
* UIButtonTypeCustom = 0, //自定义(常加载图片) no button type
* UIButtonTypeRoundedRect, //圆角按钮
* UIButtonTypeDetailDisclosure, //尖叫号按钮
* UIButtonTypeInfoLight, 信息按钮(浅)
* UIButtonTypeInfoDark, 信息按钮(深)
* UIButtonTypeContactAdd, 加号按钮
*/
/**button存在4种状态
*1⃣️normal:默认为通常状态下
*2⃣️selected:选中状态(自己设定)
*3⃣️hightLight:高亮状态(normal状态下点击不放开)
*4⃣️disabled:不可编辑状态(自己设定)
*/
//设置button上的文字
/* 1 (可访问button上的lable,对button上的字体进行设置)*/
button.titleLabel.text = @"hello world";
/* 2 */
[button setTitle:@"点我" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 设置button图片,具体细节自己观察
[button setImage:[UIImage imageNamed:@"图片名"] forState:UIControlStateNormal];
// 设置button背景图片,具体细节自己观察
[button setBackgroundImage:connectButtonBackgroundImage forState:UIControlStateNormal];
/* 设置对齐方式
*/
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
/**taget-action机制
*参数1:控件(button)
*参数2:发送消息的对象(self)
*参数3:发送消息,即执行的响应事件(onButtonClick:)
*参数4:发送消息的触发条件
*/
[button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchDown];
}
-(void)onButtonClick:(UIButton *)sender{
NSLog(@"点击事件");
}