你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS_UIButton 簡單操作

iOS_UIButton 簡單操作

編輯:IOS開發綜合

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];
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved