下面的代码执行我想要的所有内容(将editButton标题和颜色从正常颜色更改为"编辑"更改为红色"取消"然后在按下时退回除了当editButton显示红色"取消"并按下sendButton时,我希望editButton显示正常颜色"再次编辑"
我做错了什么,或者我还需要做些什么?
谢谢,
约翰
- (IBAction)sendClicked:(id)sender {
if (dontCheck) {
dontCheck = NO;
[editButton setTitle:@"Edit" forState:UIControlStateNormal];
[editButton setTitleColor:[[sendButton titleLabel] textColor] forState:UIControlStateNormal];
}
// ...
}
- (IBAction)editClicked:(id)sender {
if (dontCheck) {
dontCheck = NO;
dataLoaded = NO;
[sender setTitle:@"Edit" forState:UIControlStateNormal];
[sender setTitleColor:[[submitButton titleLabel] textColor] forState:UIControlStateNormal];
} else {
dontCheck = YES;
[sender setTitle:@"Cancel" forState:UIControlStateNormal];
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
// ...
}
这对我不起作用:(我确实在xib文件中进行了必要的更改。)
- (IBAction)sendClicked:(id)sender {
if (dontCheck) {
dontCheck = NO;
[editButton setTitle:@"Edit" forState:UIControlStateNormal];
[editButton setTitleColor:[[submitButton titleLabel] textColor] forState:UIControlStateNormal];
[editButton setSelected:NO];
}
// ...
}
- (IBAction)editClicked:(id)sender {
if (dontCheck) {
dontCheck = NO;
dataLoaded = NO;
[sender setTitle:@"Edit" forState:UIControlStateNormal];
[sender setTitleColor:[[submitButton titleLabel] textColor] forState:UIControlStateNormal];
[editButton setSelected:NO`];
} else {
dontCheck = YES;
[sender setTitle:@"Cancel" forState:UIControlStateSelected];
[sender setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[editButton setSelected:YES];
}
// ...
}
答案 0 :(得分:9)
您可以为不同的状态设置不同的颜色,例如突出显示,选中,禁用。然后,您无需更改按钮目标操作中的颜色。它还允许您简单地调用任何这些方法来更改按钮状态和外观:
[editButton setDisabled:YES];
或
[editButton setSelected:YES];
设置不同状态的颜色:
[editButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[editButton setTitleColor:[UIColor purpleColor] forState:UIControlStateHighlighted];
[editButton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
[editButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
答案 1 :(得分:0)
以下添加项满足了我的要求(在Swift中是最新版本):
@IBAction func editAction(_ sender: UIButton)
{
if (dontCheck) {
dontCheck = false
dataLoaded = false
editButton.setTitle("Edit", for: [])
editButton.setTitleColor(clearAllButton.tintColor, for: [])
self.editButton.layer.borderColor = clearAllButton.tintColor.cgColor
}
else
{
dontCheck = true
editButton.setTitle("Cancel", for: [])
editButton.setTitleColor(UIColor.red, for: [])
self.editButton.layer.borderColor = UIColor.red.cgColor
}
// ...
}
添加self.XXXButton.layer.borderColor等于另一个不变的按钮的tintColor可以解决问题。