gpt4 book ai didi

ios - 如何在不按下 iOS 按钮的情况下更改 UIButton 标题/颜色?

转载 作者:可可西里 更新时间:2023-11-01 06:23:45 32 4
gpt4 key购买 nike

下面的代码完成了我想要的一切(将 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];
}
// ...
}

最佳答案

您可以为不同的状态设置不同的颜色,例如突出显示,选中,禁用。这样您就不需要更改按钮目标操作中的颜色。它还允许您简单地调用这些方法中的任何一个来更改按钮状态和外观:

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

关于ios - 如何在不按下 iOS 按钮的情况下更改 UIButton 标题/颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8524082/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com