30

I'm making a UIButton subclass in Swift to perform custom drawing and animation on selection

What would be the equivalent in Swift of overriding - (void)setSelected:(BOOL)selected in ObjC?

I tried

override var selected: Bool

so I could implement an observer but I get

Cannot override with a stored property 'selected'

LorTush
  • 719
  • 1
  • 8
  • 14

4 Answers4

65

Like others mentioned you can use willSet to detect changes. In an override, however, you do not need assign the value to super, you are just observing the existing change.

A couple things you can observe from the following playground:

  1. Overriding a property for willSet/didSet still calls super for get/set. You can tell because the state changes from .normal to .selected.
  2. willSet and didSet are called even when the value is not changing, so you will probably want do the compare the value of selected to either newValue in willSet or oldValue in didSet to determine whether or not to animate.
import UIKit

class MyButton : UIButton {

    override var isSelected: Bool {
        willSet {
            print("changing from \(isSelected) to \(newValue)")
        }

        didSet {
            print("changed from \(oldValue) to \(isSelected)")
        }
    }
}

let button = MyButton()

button.state == .normal
button.isSelected = true // Both events fire on change.
button.state == .selected
button.isSelected = true // Both events still fire.
Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
7

you'd do it like e.g. this:

class MyButton : UIButton {

    // ...

    override var isSelected: Bool {
        willSet(newValue) {
            super.isSelected = newValue;
            // do your own business here...
        }
    }

    // ...

}
holex
  • 23,961
  • 7
  • 62
  • 76
  • 1
    Ok this works even without the public/internal keyword as suggested by diegomen. I was getting the error because I hadn't yet included the {} with the observer, silly me... But I still don't understand why they are compulsory... – LorTush Oct 14 '14 at 16:48
0

try this

override public var selected: Bool {
    willSet(selectedValue) {
        self.selected = selectedValue
        // Do whatever you want
    }
}
diegomen
  • 1,804
  • 1
  • 23
  • 37
  • Xcode gives me a warning and suggests to replace public with internal. Could you please explain why the internal keyword is necessary? Thanks. – LorTush Oct 14 '14 at 16:23
  • Using internal you're saying that you can access to the var from the same module. You got more info here: http://www.codingricky.com/access-control-in-swift/ Hope it helps :) – diegomen Oct 14 '14 at 16:35
-1

Create IBAction and check button selected or not in swift language.

@IBAction func favoriteButtonAction(sender: UIButton) {

    // Save Data
    sender.selected  = !sender.selected;

    if (sender.selected)
    {
        NSLog(" Not Selected");
    }
    else
    {
        NSLog(" Selected");
    }

}
Anit Kumar
  • 8,075
  • 1
  • 24
  • 27