2016-09-01 2 views
0

Я создал массив UIButtons, имеющий такое же действие, , если я нажму на первую кнопку, первая кнопка Цвет должен быть изменен, другая кнопка Цвет не должен изменяться. как добиться этого?UIButton set title color not working

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

} 
} 

Благодаря заранее

+0

Вы можете показать код –

+0

@anbu: у вас есть идея – NewbieiOS

+0

, в которой вы можете изменить цвет –

ответ

0

для этого вы должны принять один дополнительный UIButton объект в вас заголовок файла.

Когда пользователь нажимает на любую кнопку, вы должны сохранить выбранную кнопку в объекте refbutton и использовать, как показано ниже.

@interface YourViewController() 
{ 
UIButton *btnSelected; 
} 

Переходит к вашей Кнопке Действию:

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

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

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

Надеется, что это поможет.