2012-10-24 49 views
0

即時消息在我的應用程序中有四個按鈕,即時獲取數組中的按鈕標題,每當我點擊任何按鈕時,我都想更改4個按鈕標題,但是這裏點擊的按鈕標題只是改變了在這裏我的代碼,4按鈕標題每次點擊更改

-(IBAction)setting:(id)sender 
{ 
int value = rand() % ([arraytext count] -1) ; 
UIButton *mybuton = (UIButton *)sender; 
[mybuton setTitle:[arraytext objectAtIndex:value] forState:UIControlStateNormal]; 
} 

更新

-(IBAction)answersetting:(id)sender 
{ 

UIButton *mybutton = (UIButton *)sender; 
static int j = 0; 
if(sender == mybutton) 
    j++; 
if (j >= arcount) 
{ 
    j = 0; 
} 
else if(j < 0) 
{ 
    j = arcount - 1; 
} 

CATransition *transition = [CATransition animation]; 
transition.duration = 1.0f; 
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
transition.type = kCATruncationMiddle; 

[animage.layer addAnimation:transition forKey:nil];  
animage.image=[arimage objectAtIndex:j]; 

for(UIView *view in self.view.subviews) 
{ 
    if([view isKindOfClass:[UIButton class]]) 
    { 
     UIButton *button = (UIButton *)view; 
     if(button.tag == 1||button.tag == 2||button.tag == 3||button.tag == 4) 
     { 
      int value = rand() % ([artext count] -1) ; 
      NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init]; 
      [dictionary setValue:animage.image forKey:@"button"]; 
      [button setTitle:[artext objectAtIndex:value] forState:UIControlStateNormal]; 

     } 
    } 
} 

}

我有4個按鈕連接這種方法,我想改變這一切的按鈕標題每當我點擊任何按鈕,請幫助編碼

+0

您需要保存在陣列中的所有這些uibuttons則u可以單擊事件改變所有這些標題。 – jamil

+0

@SHAZAD可以通過代碼解釋嗎? – Fazil

回答

2

因爲sender僅包含您點擊的UIButton
要更改所有UIButton的標題,您需要一個循環。
tag設置爲全部4 buttons

那麼做到這一點 -

-(IBAction)setting:(id)sender 
{ 
    int value = rand() % ([arraytext count] -1) ; 
    for(UIView *view in self.view.subviews) 
    { 
     if([view isKindOfClass:[UIButton class]]) 
     { 
      UIButton *button = (UIButton *)view; 
      if(button.tag == 1||button.tag == 2||button.tag == 3||button.tag == 4) 
      { 
       [button setTitle:[arraytext objectAtIndex:value] forState:UIControlStateNormal]; 
      } 
     } 
    } 
} 
+0

如果我有另一個圖像數組如何顯示圖像名稱作爲標題在uibuttons – Fazil

+0

使圖像名稱數組,然後你可以從數組中獲得它們。 – TheTiger

+0

我的疑問是如何選擇一個在uiimage視圖中包含相同圖像的按鈕來進行比較? – Fazil