36

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?

Ezimet
  • 5,058
  • 4
  • 23
  • 29

3 Answers3

37

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

        }
    }

}
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
8

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

Ethan
  • 1,567
  • 11
  • 16
  • Still getting the same error " Use unresolved identifier'self' " , have you tried your code yourself ? There is nothing much going on in my class , its just UIButton subclass , I just pasted your code in the blank area , Its giving error when i try to set the background in the if statement. And , Yes i am using beta 4. – Ezimet Jul 28 '14 at 23:21
  • Ok , it is working now , I placed the code on top of the init method , it did work . Thanks for your answer. I don't know why it should be overridden before the init . Plus you have to add override keyword before it , – Ezimet Jul 28 '14 at 23:27
  • Yes, I had run this code myself (on a UIView)... Oh. But I did not realize that you were using a UIButton subclass (my fault). UIButton ALREADY has a variable called "highlighted" so, it is necessary to override this, if you wish to have a highlighted var in your own subclass. – Ethan Jul 29 '14 at 03:18
  • also you can write `self.backgroundColor = .whiteColor()` – fnc12 May 05 '15 at 12:28
3

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)
    )
 }
}
codester
  • 36,891
  • 10
  • 74
  • 72
  • 1
    you have to add override keyword before the var keyword.Please Edit your code . – Ezimet Jul 28 '14 at 23:33
  • @Ezimet i have tested the code it is working fine.No need to add `override` keyword – codester Jul 28 '14 at 23:39
  • this is strange then , the compiler warned that "Overriding declaration requires an override keyword ", Its a red error , i have to add Override keyword. – Ezimet Jul 28 '14 at 23:48
  • @Ezimet As you see i have inherited from `UIView`.There is no property `highlighted` in `UIView` class.So you can not override it.If you class is subclass of custom view or another class which have `highlighted` property than only you can override it.As your class is subclass of `UIButton` so you need to `override` it – codester Jul 29 '14 at 06:51