简体   繁体   English

UIButton设置标题颜色不起作用

[英]UIButton set title color not working

I have created the array of UIButtons , having the same action, if i click the first button, the first button Colour should be change, other button Colour should not be changed. 我创建了具有相同动作的UIButtons数组,如果我单击第一个按钮,则应更改第一个按钮的颜色,不应更改其他按钮的颜色。 how to achieve this? 如何做到这一点?

for(titleStr in titleArray){

        actionButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
        [actionButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        actionButton.tag = count;
        [actionButton setTitle:titleArray[count] forState:UIControlStateNormal];

[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
        CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
        CGRect buttonframe;
        buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
        actionButton.frame = buttonframe;
        [titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
        [contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
        if(count==0){
            actionButton.titleLabel.font=[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0];
            [actionButton setSelected:YES];
            tempObj = actionButton;
        }else {
            actionButton.titleLabel.font=[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0];

        }
        [titleScrollView addSubview:actionButton];
        contentOffset += actionButton.frame.size.width+5.0;
        titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
        // Increase the count value
        count++;

    }

-(void)buttonTapped:(id)sender { 

if([sender tag]==0){ 
UIButton *tappedButton = (UIButton *)sender; 

[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal]; 
actionButton.titleLabel.textColor = [UIColor whiteColor]; 

[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled]; 
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 

} 
}

Thanks advance 谢谢前进

for that you have to take one extra UIButton Object in you header file. 为此,您必须在头文件中添加一个额外的UIButton对象。

While user click on any button you have to store that selected Button in refbutton object and use like below. 当用户单击任何按钮时,您必须将该选定的按钮存储在refbutton对象中,并按如下方式使用。

@interface YourViewController () 
{
 UIButton *btnSelected;
}

Now Move to your Button Action: 现在移至您的按钮动作:

-(IBAction)buttonTapped:(UIButton *)sender { 

    if(_btnSelected){
      _btnSelected.titleLabel.textColor = [UIColor blackColor]; 
    }
   _btnSelected = sender;

  sender.titleLabel.textColor = [UIColor whiteColor]; 
} 

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM