Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change title color of UIButton when clicked? [duplicate]

Tags:

ios

I have a view and I created some 8 buttons programmatically in that. The title color of buttons is white color. I want to change color of button title to green color when it is clicked. And if i click any other button, the previously clicked button title color should become white and current button title color should become green.

How to do that?

like image 937
Surendra Avatar asked Oct 09 '13 09:10

Surendra


1 Answers

Initialize all your buttons like this

[mybutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[mybutton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
[mybutton addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];

then change the selected state of you button when clicked

-(void)onclick:(id)sender{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
like image 94
Bordz Avatar answered Oct 03 '22 14:10

Bordz