4

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")
        }
    }
}
Berty86
  • 637
  • 6
  • 13

2 Answers2

8

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.

Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
0

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
        }
    }
chAOS
  • 11
  • 5