ios - xcode button action swift




Objective C Setter overriding in Swift (2)

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?


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

        }
    }

}

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







swift