【问题标题】:Change Button Text Color Programmatically for n number of buttons without changing Text Color of other button以编程方式更改 n 个按钮的按钮文本颜色,而不更改其他按钮的文本颜色
【发布时间】:2015-02-02 06:39:47
【问题描述】:

我在滚动视图中有 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];
            }
        }            
    }
}

【问题讨论】:

    标签: ios objective-c uibutton tags textcolor


    【解决方案1】:
    • 应用状态虎钳(Normal,Selected)按钮的标题颜色。
    • 只需将选定的按钮引用保持为弱。
    • 当用户单击按钮时,将按钮状态设置为选中状态而不是正常状态。
    • 更改上次选择的按钮状态正常而不是已选择

    查看以下对您有帮助的代码

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

    【讨论】:

      【解决方案2】:

      我不了解您的 adButtonAction(什么是 menuArray?)方法中的所有内容,但我认为您需要的很简单,只需根据您的方法调整即可。

      首先创建一个NSMutableArray 以在您的按钮列表中保留引用:

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

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

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

      【讨论】:

        【解决方案3】:

        您为什么不尝试将所有按钮样式更改为未选择,除了发送者参数上的那个(选定按钮)?

        -(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 是一个包含所有按钮的数组。 这样,您可以在按下按钮时检查和修改所有样式。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-21
          • 2017-04-10
          • 2011-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多