Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override UIButton's setEnabled method in Swift

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

like image 560
Andrey Gordeev Avatar asked Jan 27 '15 13:01

Andrey Gordeev


People also ask

Can we override class method in Swift?

Classes in Swift can call and access methods, properties, and subscripts belonging to their superclass and can provide their own overriding versions of those methods, properties, and subscripts to refine or modify their behavior.

How do you override a computed property in Swift?

A computed property can also be overridden in a class by adding override to the property in the extending class.

How do you override a variable in Swift?

We use the override keyword to declare method overriding. For example, class Vehicle { func displayInfo(){ ... } } class Car: Vehicle { // override method override func displayInfo() { ... } }

What is override func in Swift?

Swift version: 5.6. The override is used when you want to write your own method to replace an existing one in a parent class. It's used commonly when you're working with UIViewControllers , because view controllers already come with lots of methods like viewDidLoad() and viewWillAppear() .


1 Answers

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
    }
}
like image 106
bzz Avatar answered Sep 29 '22 05:09

bzz