2014-04-23 61 views
0

喜在我的應用程序要求用戶提供反饋。例如,這可以評估我們的服務器。他們通過點擊兩個按鈕中的一個來做到這一點,無論好壞。當他們選擇一個選項時,按鈕的圖像會發生變化,這是我需要的。我的問題是,如果他們選擇好,然後選擇好壞按鈕需要恢復到「未選擇的」選項。作爲被「選擇」如何通過點擊另一個按鈕取消選擇的形象呢?

我的按鈕代碼不能打擾。

- (IBAction)gd:(id)sender { 
    UIButton *btn = (UIButton *)sender; 

    if ([sender isSelected]) { 
     [btn setBackgroundImage:[UIImage imageNamed:@"rd.png"] forState:UIControlStateNormal]; 
     [sender setSelected:NO]; 
    } 
    else{ 
     [sender setBackgroundImage:[UIImage imageNamed:@"rd1.png"] forState:UIControlStateNormal]; 
     [sender setSelected:YES]; 
    } 
} 

- (IBAction)bd:(id)sender { 
    UIButton *btn1 = (UIButton *)sender; 

    if ([sender isSelected]) { 
     [btn1 setBackgroundImage:[UIImage imageNamed:@"rd.png"] forState:UIControlStateNormal]; 
     [sender setSelected:NO]; 
    } 
    else{ 
     [sender setBackgroundImage:[UIImage imageNamed:@"rd1.png"] forState:UIControlStateNormal]; 
     [sender setSelected:YES]; 

    } 
} 

我已經使用上述,但它不工作就像我想它顯示在下面的圖像。

enter image description here

請告訴我,表明解決這一之一。

+0

請重新輸入你的問題,使用標點符號。它是不可讀的。 – cyborg86pl

+0

@ cyborg86pl我已經編輯我的問題,現在請您檢查 – user3427551

回答

0

這裏是您的解決方案

添加此功能在啓動,取消選擇2個按鈕,只要你在按鈕點擊: -

-(void)deselectButtons 
{ 
    for(UIView *vw in [self.view subviews]) 
    { 
     if([vw isKindOfClass:([UIButton class])]) 
     { 
      UIButton *btn=(UIButton *)vw; 
      btn.selected=FALSE; 
     } 
    } 
} 

良好的按鈕: -

- (IBAction)bd:(id)sender { 

    [self deselectButtons];//add this line of code 

    UIButton *btn1 = (UIButton *)sender; 

    if ([sender isSelected]) { 
     [btn1 setBackgroundImage:[UIImage imageNamed:@"rd.png"] forState:UIControlStateNormal]; 
       [sender setSelected:NO]; 
    } else { 
     [sender setBackgroundImage:[UIImage imageNamed:@"rd1.png"] forState:UIControlStateNormal]; 
     [sender setSelected:YES]; 
    } 

} 

壞按鈕: -

- (IBAction)bd:(id)sender { 

    [self deselectButtons];//add this line of code  

    UIButton *btn1 = (UIButton *)sender; 

    if ([sender isSelected]) { 
     [btn1 setBackgroundImage:[UIImage imageNamed:@"rd.png"] forState:UIControlStateNormal]; 
     [sender setSelected:NO]; 
    } else { 
     [sender setBackgroundImage:[UIImage imageNamed:@"rd1.png"] forState:UIControlStateNormal]; 
     [sender setSelected:YES]; 
    } 
} 
+0

@Vizilx我不想取消勾選,請閱讀我的問題proplerly我想delesect之一,當其他按鈕被選中 – user3427551

2

爲兩個按鈕都創建一個插座併爲這兩種狀態設置圖像。然後

- (void)initButtons { 
    [gdBtn setBackgroundImage:[UIImage imageNamed:@"rd.png"] forState:UIControlStateNormal]; 
    [gdBtn setBackgroundImage:[UIImage imageNamed:@"rd1.png"] forState:UIControlStateSelected]; 
    [bdBtn setBackgroundImage:[UIImage imageNamed:@"rd.png"] forState:UIControlStateNormal]; 
    [bdBtn setBackgroundImage:[UIImage imageNamed:@"rd1.png"] forState:UIControlStateSelected]; 
} 

的行動應該是這樣的:

- (IBAction)gd:(UIButton *)sender { 
    [sender setSelected:!sender.isSelected]; 
    [bdBtn setSelected:NO]; 
} 

- (IBAction)bd:(UIButton *)sender { 
    [sender setSelected:!sender.isSelected]; 
    [gdBtn setSelected:NO]; 
} 
+0

我曾嘗試在烏爾代碼isSelect其示值誤差像財產isSelect不發現 – user3427551

相關問題