【发布时间】:2014-09-19 04:04:43
【问题描述】:
在 UIButton 子类中:
- (void) setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundColor = [UIColor redColor];
}
else {
self.backgroundColor = [UIColor blueColor];
}
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
self.backgroundColor = [UIColor greenColor]; //this happens
}
else {
self.backgroundColor = [UIColor blueColor];
}
}
当按钮突出显示时,我看到红色。但是当我设置 button.selected = YES 时,我从来没有看到绿色。为什么 setHighlighted 有效而 setSelected 无效?
我可以在“发生这种情况”行设置一个断点,它就会发生。但按钮的背景颜色不会变为绿色。
【问题讨论】:
-
您在界面生成器中设置了哪些属性?
-
那里有几个答案。 stackoverflow.com/questions/4049103/… 看起来一个常见的解决方案是设置一个基本上是 1x1 像素颜色的背景图像。此解决方案有效,因为您为每个特定状态设置了背景图像。我不太确定为什么您的代码不起作用。按钮被选中后是否立即取消选中?