Override UIButton's setEnabled method in Swift
Asked Answered
C

2

14

In Swift, UIControl doesn't seem to have a setEnabled: method. Is there a way to detect when the control state was changed?

Clementia answered 27/1, 2015 at 13:47 Comment(0)
W
36

You can do something like that in your subclass:

override var enabled:Bool {
    didSet {
        //Your code
    }
}

Swift 3.0

override var isEnabled:Bool {
    didSet {
        //Your code
    }
}
Wera answered 27/1, 2015 at 13:55 Comment(3)
Thanks! Completely forgot that we can override properties in SwiftClementia
I was wondering if we could ovveride properties in swift the moment I fell on this!!!Timmons
Just a note that you do not need to call super here - Swift does that automatically when overriding properties.Maddiemadding
E
4

In swift 3 it is now:

override var isEnabled: Bool {
    didSet {
        //Your code
    }
}
Eurus answered 17/11, 2016 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.