目标C Setter覆盖Swift

时间:2014-07-28 22:18:59

标签: ios uibutton swift

我需要在自定义UIButton子类中覆盖UIViews突出显示属性的setter;

目标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中实现此方法以获得相同的结果。任何想法?

3 个答案:

答案 0 :(得分:37)

Swift 中,上面的解决方案对我有用,但我必须忽略 Bool = true

import UIKit

class CustomUIButtonForUIToolbar: UIButton {

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        super.drawRect(rect)

        self.layer.borderColor = UIColor.blueColor().CGColor
        self.layer.borderWidth = 1.0
        self.layer.cornerRadius = 5.0
        self.clipsToBounds = true
        self.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

        self.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
    }

    override var highlighted: Bool {
        didSet {

            if (highlighted) {
                self.backgroundColor = UIColor.blueColor()
            }
            else {
                self.backgroundColor = UIColor.clearColor()
            }

        }
    }

}

答案 1 :(得分:8)

这里有一些问题,但这个解决方案可以帮助你...

在您的情况下,由于您并不真的想要变量 突出显示 的计算值,而是您想要知道突出显示何时更改,您应该使用< em> willSet 或 didSet

对于您的情况, didSet

看起来像这样

var highlighted:Bool = false {
    didSet {
        // You can use 'oldValue' to see what it used to be,
        // and 'highlighted' will be what it was set to.
        if highlighted
        {
            self.backgroundColor = UIColor.whiteColor()
        } else
        {
            self.backgroundColor = UIColor.blackColor()
        }
}
}

请记住,初始值设置为false但是初始化变量不会调用didSet块(我不认为),因此默认使用backgroundColor black ...或其他任何内容初始化此视图。

Swift ibook有关于set,get,didSet和willSet的一些很好的提示,大概是在250页左右。

如果您的错误仍然存​​在,请告诉我(如果是这样,您可能会在设置此变量和类标题和内容时发布,可能信息不足。还有,您使用的是xcode6-beta4吗?)

答案 2 :(得分:3)

您正在使用computed properties而不是stored property。因为swift中没有提供UIColorFromRGB所以我写了我的

class ViewSubClass:UIView{

var highlighted: Bool = true {

   didSet {

      if (highlighted) {
    self.backgroundColor = UIColorFromRGB(0x387038);
      }
      else {
         self.backgroundColor = UIColorFromRGB(0x5bb75b);
      }
   }


}


init(frame: CGRect) {

    super.init(frame: frame)
}

func UIColorFromRGB(rgbValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
 }
}
相关问题