This question already has an answer here:
这个问题已经有了答案:
- How can I change UIButton title color? 5 answers
- 如何更改UIButton标题颜色?5个回答
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.
我有一个视图,我用编程的方式创建了8个按钮。按钮的标题颜色是白色。我想把按钮标题的颜色改为绿色。如果我点击任何其他按钮,先前点击的按钮标题颜色应该变成白色,当前按钮标题颜色应该变成绿色。
How to do that?
如何做呢?
3 个解决方案
#1
11
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;
}
#2
0
Create IBAction
for all of the buttons, create a property @property (strong, nonatomic) UIButton *currentButton
. In the action do the following:
为所有按钮创建IBAction,创建属性@property(强属性,非原子属性)UIButton *currentButton。在行动中做以下事:
-(IBAction)buttonClicked:(id)sender
{
UIButton *buttonSender = (UIButton *)sender;
[self.currentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.currentButton = buttonSender;
[self.currentButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
#3
0
Set control state of buttons :
设置按钮控制状态:
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
#1
11
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;
}
#2
0
Create IBAction
for all of the buttons, create a property @property (strong, nonatomic) UIButton *currentButton
. In the action do the following:
为所有按钮创建IBAction,创建属性@property(强属性,非原子属性)UIButton *currentButton。在行动中做以下事:
-(IBAction)buttonClicked:(id)sender
{
UIButton *buttonSender = (UIButton *)sender;
[self.currentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.currentButton = buttonSender;
[self.currentButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
#3
0
Set control state of buttons :
设置按钮控制状态:
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
[btnOk setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];