typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead
};
簡單操作
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.tag = 100; // 給 Button 添加標記 在不同的方法中使用同一個控件
button.frame = CGRectMake(30, 170, 200, 40);
[self.window addSubview:button];
// 設置按鈕文字,需要設置狀態
[button setTitle:@"按鈕" forState:UIControlStateNormal];
// [button setTitle:@"Hello" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; // 給字體設置顏色
button.showsTouchWhenHighlighted = YES; // 高亮
// 給button綁定事件
[button addTarget:self // 接受消息的對象
action:@selector(buttonAction:) // 發送的消息
forControlEvents:UIControlEventTouchUpInside]; // 觸發的方式
- (void) buttonAction
{
NSLog(@"咔");
// 通過 tag ,從父類視圖中獲取button
UIButton *btn = (UIButton *)[self.window viewWithTag:100];
[btn setTitle:@"點我" forState:UIControlStateNormal];
// 讓 Button 失效
[btn removeTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void) buttonAction:(UIButton *)sender
{
sender.backgroundColor = [UIColor redColor];
}