Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C Setter overriding in Swift

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

Objective C

@property(nonatomic,getter=isHighlighted) BOOL highlighted; 

overridden like this

- (void) setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

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

   [super setHighlighted:highlighted];

}

Swift

var highlighted: Bool 

I tried:

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 
        }

        }

   }

How and where should implement this method in Swift to get the same result . any idea?

like image 661
Ezimet Avatar asked Jul 28 '14 22:07

Ezimet


3 Answers

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

        }
    }

}
like image 122
King-Wizard Avatar answered Nov 03 '22 09:11

King-Wizard


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?)

like image 26
Ethan Avatar answered Nov 03 '22 08:11

Ethan


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)
    )
 }
}
like image 3
codester Avatar answered Nov 03 '22 09:11

codester