'UIViewController isEditing - Property observer doesn't work
I was trying to implement property observers on my custom UIViewController but I noticed it was not working with the isEditing property.
Do you guys have an idea why?
class MasterViewController: UIViewController {
// MARK: - Properties
override var isEditing: Bool {
didSet {
print("VC is editing")
}
}
}
Solution 1:[1]
According to the documentation for isEditing
Use the setEditing(_:animated:) method as an action method to animate the transition of this state if the view is already displayed.
And from setEditing(_:animated:)
Subclasses that use an edit-done button must override this method to change their view to an editable state if isEditing is true and a non-editable state if it is false. This method should invoke super’s implementation before updating its view.
TL;DR
You'll want to override setEditing(_:animated:) instead.
Solution 2:[2]
It's for those who can't find any example how setEditing works.
SWIFT 5 :
override func setEditing(_ editing: Bool, animated: Bool) {
if yourTableView.isEditing == true {
yourTableView.isEditing = false //change back
} else {
yourTableView.isEditing = true // activate editing
editButtonItem.isSelected = true // select edit button
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Craig Siemens |
| Solution 2 | AOS |
