iOS_UIButton 簡單操作
UIButton 風格
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]; // 觸發的方式
Button 方法
- (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];}