gpt4 book ai didi

iOS Xcode : UIButton titleLabel color keeps changing

转载 作者:行者123 更新时间:2023-11-28 18:32:53 24 4
gpt4 key购买 nike

场景 = 我有一个带有 titleLable(文本)的 UIButton,它的特性会根据 bool 值是 YES 还是 NO 而改变。我在 Storyboard中将该按钮的 titleLabel textColor 设置为白色。现在,以编程方式,如果 bool 值设置为 NO,我希望该颜色变为红色。

所有这些工作正常。

问题 = 当我在将按钮设置为红色后点击该按钮时,当我抬起手指时,按钮上的 titleLabel 变回 为白色。

问题 = 如何将 titleLabel 的颜色设置为正确的颜色并在 bool 变量状态的整个持续时间内保持正确的颜色?

//CODE
//Button text set to 'white' in storyboard


if ([[NSUserDefaults standardUserDefaults]boolForKey:@"bool"]==YES) {

self.button.backgroundColor = [UIColor whiteColor];
self.button.titleLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue"]];

}

else {

self.button.backgroundColor = [UIColor whiteColor];
self.button.titleLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red"]];

}

Fubar 的顺序是这样的...

1 drag UIButton onto view controller
2 set UIButton titleLabel textColor to white (storyboards)
3 in view controller .m viewDidLoad I type the code above
4 I run the program on my iPhone
5 press button (bool = NO and textColor is red)
6 (.00000000001 seconds after #5) I lift my finger from the button and the titleLabel textColor changes back to the white color that is set to in storyboard.
7 frustration ensues

最佳答案

代替 self.button.titleLabel.text = ...

尝试

[self.button setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"red"]] forState:UIControlStateNormal];

关于iOS Xcode : UIButton titleLabel color keeps changing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24518569/

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