Objective C Setter overriding in Swift

后端 未结 3 1719
情歌与酒
情歌与酒 2020-12-24 11:36

I need to override the setter of UIViews highlighted property in my custom UIButton subclass ;

Objective C

@property(nonatomic,getter=isHighlighted)          


        
相关标签:
3条回答
  • 2020-12-24 12:01

    In Swift the solution above worked for me, but I had to omit the 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()
                }
    
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-24 12:09

    There are a couple things wrong here, but this solution should help you...

    In your case, since you do not really want computed values for the variable highlighted, but rather all you want is to know when highlighted changed, you should use willSet or didSet

    for your case, didSet.

    it looks like this

    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()
            }
    }
    }
    

    Keep in mind that the initial value is set to false BUT initializing the variable does not call the didSet block (i don't think), so default initialize this view with backgroundColor black... or whatever.

    the Swift ibook has some good tips about set, get, didSet and willSet, around page 250 ish.

    Let me know if your error remains, (and if so you should maybe post when you set this variable & the class headers and stuff, may not be enough information. Also, are you using xcode6-beta4?)

    0 讨论(0)
  • 2020-12-24 12:11

    You are using computed properties instead use stored property.As there is no UIColorFromRGB provided in swift so i have written mine

    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)
        )
     }
    }
    
    0 讨论(0)
提交回复
热议问题