【问题标题】:4 button title change on each click每次点击 4 个按钮标题更改
【发布时间】:2012-10-24 09:54:39
【问题描述】:

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

【问题讨论】:

  • 您需要将所有这些 uibuttons 保存在数组中,然后您可以在点击事件时更改所有这些按钮的标题。
  • @SHAZAD 可以用代码解释一下吗?

标签: iphone ios uibutton nsmutablearray


【解决方案1】:

因为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];
            }
        }
     }  
}

【讨论】:

  • 如果我有另一个图像数组,我如何在 uibuttons 中将图像名称显示为标题
  • 制作图片名称数组,然后你可以从数组中获取它们。
  • 我的疑问是如何比较是否在 uiimage 视图中选择了包含相同图像的按钮?
  • 我没有正确理解您。您只有一种方法可以点击所有按钮...那么您的 imageView 怎么样?
  • 我正在询问我是否将图像包含在数组中,以及是否将按钮的标题显示为图像名称...我该如何继续?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2012-05-15
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多