【发布时间】:2014-07-28 22:18:58
【问题描述】:
我需要在我的自定义 UIButton 子类中覆盖 UIViews 高亮属性的设置器;
目标 C
@property(nonatomic,getter=isHighlighted) BOOL highlighted;
像这样被覆盖
- (void) setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundColor = UIColorFromRGB(0x387038);
}
else {
self.backgroundColor = UIColorFromRGB(0x5bb75b);
}
[super setHighlighted:highlighted];
}
斯威夫特
var highlighted: Bool
我试过了:
var highlighted: Bool {
get{ return false }
set {
if highlighted {
self.backgroundColor = UIColor.whiteColor()
//Error "Use unresolved identifier 'self'"
I can't set the background color from value type in here
, can't call self.backgroundColor in this value type ,
can't call super too because this is a value type , doesn't work
}
}
}
在 Swift 中应该如何以及在何处实现此方法以获得相同的结果。有什么想法吗?
【问题讨论】:
-
什么不起作用?您没有将 backgroundColor 设置为任何内容...