2014-10-31 97 views
0

使用方法:IOS - 設置/取消按鈕的背景顏色

- (IBAction)buttonClicked:(id)sender { 

    mybutton.backgroundColor = [UIColor blackColor]; 

} 

我可以設置黑色到myButton的。如何在兩種顏色之間切換,每次點擊按鈕?

的Tx提前

回答

0

在您的.h文件初始化布爾屬性

@property (nonatomic, assign) BOOL myBool; 

在您的M檔把下面一行在viewDidLoad中

self.myBool = YES; 

然後在你的IBAction爲,

- (IBAction)buttonClicked:(id)sender 
{ 
    if (self.myBool) 
    { 
     self.button.backgroundColor = [UIColor blackColor]; 
     self.myBool = NO; 
    } 
    else 
    { 
     self.button.backgroundColor = [UIColor blackColor]; 
     self.myBool = YES; 
    } 
}