2014-04-04 34 views
0

是否有人知道如何使用IBOutletCollection來設置按鈕數組中每個單獨按鈕的標題?這是我試過的,但代碼設置了所有按鈕的標題。我已將插座連接到按鈕並設置其各自的標籤。在IBOutletCollection中設置單個標題的UIButtons

.h file 
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttonCollection; 

.m file 
- (IBAction)switchAction:(id)sender { 

    for (UIButton *btn in buttonCollection) { 
     if (btn.tag == 0) { 
      [btn setTitle:@"1st Button" forState:UIControlStateNormal]; 
     } else if (btn.tag == 1) { 
      [btn setTitle:@"2nd Button" forState:UIControlStateNormal]; 
     } else if (btn.tag == 2) { 
      [btn setTitle:@"3rd Button" forState:UIControlStateNormal]; 
     } 
} 
+1

這是什麼將標題設置爲?你是說所有的按鈕都有相同的標題嗎? – rdelmar

+0

不,每當我點擊一個按鈕代碼立即設置所有按鈕的標題:按鈕1設置爲第一按鈕和按鈕2設置爲第二按鈕等。很抱歉,如果我是'我的問題是足夠清楚。 –

+0

你想要發生什麼?只設置你點擊的按鈕的標題? – rdelmar

回答

1

如果你只是想設置你點擊按鈕的標題,擺脫for循環,

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

    if (sender.tag == 0) { 
      [sender setTitle:@"1st Button" forState:UIControlStateNormal]; 
    } else if (sender.tag == 1) { 
      [sender setTitle:@"2nd Button" forState:UIControlStateNormal]; 
    } else if (sender.tag == 2) { 
     [sender setTitle:@"3rd Button" forState:UIControlStateNormal]; 
} 
+0

優秀!非常感謝你。它完美地工作:-) –

0

試試這個:

for (int i = 0; i < [buttonCollection count]; ++i) 
{ 
    [((UIButton*)self.buttonCollection[i]) setTitle: [NSString stringWithFormat: @"%d button", i] forState: UIControlStateNormal]; 
} 
+0

感謝您的快速回復,但它沒有做我想做的事。見上面的評論。對不起,如果我不太清楚。它現在的作品:見第二回答:) –

相關問題