2012-10-24 48 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