简体   繁体   English

UIViewController isEditing - 属性观察器不起作用

[英]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.我试图在我的自定义 UIViewController 上实现属性观察器,但我注意到它不适用于 isEditing 属性。

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 根据isEditing的文档

Use the setEditing(_:animated:) method as an action method to animate the transition of this state if the view is already displayed. 如果已显示视图,请使用setEditing(_:animated :)方法作为操作方法来动画化此状态的过渡。

And from setEditing(_:animated:) 并从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. 如果isEditing为true,则使用edit-done按钮的子类必须重写此方法,以将其视图更改为可编辑状态;如果为false,则将其视图更改为不可编辑状态。 This method should invoke super's implementation before updating its view. 此方法应在更新其视图之前调用super的实现。


TL;DR TL; DR

You'll want to override setEditing(_:animated:) instead. 您将要改写setEditing(_:animated:)

It's for those who can't find any example how setEditing works.它适用于那些无法找到 setEditing 工作原理示例的人。

SWIFT 5: 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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSNotificationCenter观察器不起作用 - NSNotificationCenter observer doesn't work Swift isEditing 模式下的 Tableview 不会取消选择单元格 - Swift Tableview in isEditing mode doesn't deselect cell 从SKScene到UIViewController的Segue不起作用 - Segue from SKScene to UIViewController doesn't work Swift:通过NSNotificationCenter的键盘观察器不起作用 - Swift: Keyboard Observer via NSNotificationCenter doesn't work Swift协议定义的初始化不适用于UIViewController - Swift protocol defines an init that doesn't work for UIViewController 具有Storyboard的UIViewController子类的子类无法正常工作 - Subclass from the subclass of UIViewController with Storyboard doesn't work as except 当前存在的iOS 10 Swift 3 UIViewController不起作用 - iOS 10 Swift 3 UIViewController present doesn't work 如何在isEditing属性更改时更新UITableView? - How to update UITableView when isEditing property changes? 为什么 Property Observer 不适用于 SwiftUI 中的 Picker - Why Property Observer does not work for Picker in SwiftUI 如果按钮在自定义视图中使用 let 和 addTarget 初始化,则 UIButton 的操作不起作用,但它在 UIViewController 中起作用 - Action of UIButton doesn't work if the button is initialized with let and the addTarget in it in custom view, but it works in UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM