2016-01-19 85 views
0

我有2個按鈕與按下的圖片保持一致,即使它們被釋放,直到按下另一個按鈕,然後圖像返回到正常圖像我的代碼是這樣的:不能在按下其他按鈕之後改變圖片的按鈕

- (IBAction)button1_touch:(UIButton *)sender 
{ 

    [Button2 setImage:[UIImage imageNamed:@"b2Released.png"] forState:UIControlStateNormal]; 

    [Button1 setImage:[UIImage imageNamed:@"b1Pressed.png"] forState:UIControlStateNormal];  
} 

- (IBAction)button2_touch:(UIButton *)sender 
{ 
    [Button1 setImage:[UIImage imageNamed:@"b1Released.png"] forState:UIControlStateNormal]; 

    [Button2 setImage:[UIImage imageNamed:@"b2Pressed.png"] forState:UIControlStateNormal]; 


} 

應用程序啓動時,上面的代碼工作正常,是預期的行爲按兩個按鈕,圖像正確地更改,但是當我按下另一個按鈕,除了這兩項,則「將Button2」一旦獲得了「B2Pressed .png「圖像,它永遠不會返回到它發佈的圖像,我在button1_touch中寫了一個nslog,並且按預期打印了它,並且Button1獲取了它的壓縮圖像,但Button2沒有獲取其發佈的圖像,就像setImage UiButton被破壞ñ我按其他按鈕比這兩個,其他按鈕是獨立於這兩個,對我來說,這種行爲是完全沒有意義的,它可能是xcode破碎?我工作的xcode 7.2

編輯: 當我按另一個按鈕時,只有當該按鈕有一個動作選擇器分配給它(UIControlEventTouchUpInside)時發生問題。

回答

2

可以使用的UIButton的selected狀態爲此目的:

- (void)viewDidLoad { 
    // ... 
    // the place where you do buttons setup 
    [button1 setImage:[UIImage imageNamed:@"b1Released"] forState:UIControlStateNormal]; 
    [button1 setImage:[UIImage imageNamed:@"b1Pressed"] forState:UIControlStateSelected]; 
    [button2 setImage:[UIImage imageNamed:@"b2Released"] forState:UIControlStateNormal]; 
    [button2 setImage:[UIImage imageNamed:@"b2Pressed"] forState:UIControlStateSelected]; 
}  

- (IBAction)firstButtonTouch:(UIButton *)sender { 
    button1.selected = YES; 
    button2.selected = NO; 
} 

- (IBAction)secondButtonTouch:(UIButton *)sender { 
    button1.selected = NO; 
    button2.selected = YES; 
} 
+0

代碼工作完美 – Jose

相關問題