【发布时间】:2014-07-01 20:15:05
【问题描述】:
场景 = 我有一个 UIButton,上面有一个 titleLable(文本),它的特性会根据布尔值是 YES 还是 NO 而改变。我在情节提要中将该按钮的 titleLabel textColor 设置为白色。现在,以编程方式,如果布尔值设置为 NO,我希望该颜色变为红色。
这一切都很好。
问题 = 当我在按钮设置为红色后点击按钮时,抬起手指时,按钮上的 titleLabel 将 返回 变为白色。
问题 = 如何将我的 titleLabel 的颜色设置为正确的颜色,并在布尔变量状态的整个持续时间内保持正确的颜色?
//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
【问题讨论】:
-
你能发布一些你迄今为止尝试过的代码吗?当我们不知道您尝试了什么时,很难告诉您出了什么问题……
-
button.titleLable.textColor = [UIColor redColor]; --- 它变为红色,但在启动应用程序并按下按钮时,按钮会在触摸时变回白色。
-
我的意思是一些代码,比如:你在哪里改变布尔值?你在哪里检查布尔值?你在哪里设置颜色?等
标签: ios objective-c xcode uibutton