每次点击都会更改4个按钮标题

时间:2012-10-24 09:54:40

标签: iphone ios uibutton nsmutablearray

我的应用程序中有四个按钮,我从数组中获取按钮标题,我想在每次单击任何按钮时更改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个按钮连接此方法,我想在每次点击任何按钮时更改所有按钮标题,请帮助编码

1 个答案:

答案 0 :(得分: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];
            }
        }
     }  
}