更改按钮文本颜色以编程方式为n个按钮而不更改其他按钮的文本颜色

时间:2014-12-04 11:38:49

标签: ios objective-c uibutton tags textcolor

我在scrollView中有n个按钮。如果单击按钮,其文本颜色正在更改,但如果单击另一个按钮,则前一个按钮的文本颜色保持不变。我想在单击另一个按钮时将前一个按钮文本颜色更改为默认颜色。行为将像分段控件一样。请帮我解决这个问题,我在下面提供了我的代码:

-(void) loadScrollView:(CGRect)scrollViewFrame withButtonArray:(NSArray*)buttonArray withCase: (int)ButtonCase
{
    scrollView=[[UIScrollView alloc]initWithFrame:scrollViewFrame];
    [scrollView setScrollEnabled:YES];
    [scrollView setBackgroundColor:[UIColor blackColor]];
    [scrollView setContentSize:CGSizeMake(100 * 768, 40)];

    for (int i = 0; i < [buttonArray count]; i++)
    {
        adButtonOutLet = [[UIButton alloc] initWithFrame:CGRectMake(140*i, 0, 135, 40)];
        if (ButtonCase==0) {
            [adButtonOutLet setBackgroundColor:UIColorFromRGB(0X272c2f)];
            [adButtonOutLet setTitleColor:UIColorFromRGB(0x969696) forState:UIControlStateNormal];
        }
        else
        {
            if (i==0) {
                adButtonOutLet.backgroundColor=UIColorFromRGB(0x000000) ;
                [adButtonOutLet setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal];
            }            
        }

        adButtonOutLet.titleLabel.font=[UIFont fontWithName:@"MyriadPro" size:14.0];
        [adButtonOutLet setTitle:[buttonArray objectAtIndex:i] forState:UIControlStateNormal];
        adButtonOutLet.userInteractionEnabled= YES;
        [adButtonOutLet setTag:i];
        [adButtonOutLet addTarget:self action:@selector(adButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        [scrollView addSubview:adButtonOutLet];
        [self.view addSubview:scrollView];
    }
}

以下是我的操作方法:

 -(void)adButtonAction:(UIButton*)sender
{
    for (int i = 0; i < [menuArray count]; i++)
    {
        int prevTag = 0;
        if (sender.tag == i && Case==0)
        {                
            [self reloadScrollViewwithButtonTag:i];
           // [sender setSelected:YES];
            sender.backgroundColor=UIColorFromRGB(0x000000) ;
            [sender setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal];
            prevTag=i;
        }

        if (Case==1) {
            sender.backgroundColor=UIColorFromRGB(0x000000) ;
            [sender setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal];
            if (sender.tag==prevTag-1) {
                [sender setBackgroundColor:UIColorFromRGB(0X272c2f)];
                [sender setTitleColor:UIColorFromRGB(0x969696) forState:UIControlStateNormal];
            }
        }            
    }
}

3 个答案:

答案 0 :(得分:2)

为什么不尝试将所有按钮样式更改为除发送者参数(选定按钮)上的按钮之外的所有按钮样式?

-(void)adButtonAction:(UIButton*)sender{
    for (int i = 0; i < [menuArray count]; i++)
    {
        if (sender == menuArray[i])
        {
            //Selected code style
        }

        else{
            //No selected code style
        }
    }
}

考虑menuArray是一个包含所有按钮的数组。 这样,您可以在按下按钮时检查和修改所有样式。

希望这可以帮助您,或者最后为您提供解决问题的线索。

答案 1 :(得分:1)

我不喜欢&#39;了解adButtonAction(什么是menuArray?)方法中的所有内容,但我认为您需要的是简单的,只需根据您的方法进行调整。

首先创建一个NSMutableArray以保持对按钮列表的引用:

for (int i = 0; i < [buttonArray count]; i++)
{
    adButtonOutLet = [[UIButton alloc] initWithFrame:CGRectMake(140*i, 0, 135, 40)];
    [myButtonArray addObject:adButtonOutlet];
....

然后在你的动作方法中,设置正确的颜色:

for (UIButton* b in myButtonArray){
    if(b.tag == sender.tag){
         [self setSelectedColor:YES forButton:b];
         // Do what you want here
    }
    else{
         [self setSelectedColor:NO forButton:b];
    }
}

使用:

-(void)setSelectedColor:(BOOL)selected forButton:(UIButton)button{
     if(selected){
        sender.backgroundColor=UIColorFromRGB(0x000000) ;
        [sender setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal];
     }
     else{
         [sender setBackgroundColor:UIColorFromRGB(0X272c2f)];
         [sender setTitleColor:UIColorFromRGB(0x969696)forState:UIControlStateNormal];
     }
}

答案 2 :(得分:1)

  • 应用按钮的状态虎钳(正常,选定)标题颜色。
  • 只是将选定的按钮参考保持为弱。
  • 当用户点击按钮时,将该按钮状态设置为正常选择。
  • 更改上次选择的按钮状态正常,而不是已选择

查看以下可以帮助您的代码

UIButton *selectedButton;
-(void)adButtonAction:(UIButton*)sender
{
        UIButton *tempButton = sender;
        if (selectedButton && selectedButton!=tempButton)
        {
            [selectedButton setSelected:NO];
        }
        [tempButton setSelected:YES];
        selectedButton = tempButton;
}