简体   繁体   中英

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")
        }
    }
}

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.

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
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM