ios - 在 UITableViewController 的 UINavigationItem 栏中从 "Done"更改后按下 "Edit"时触发什么?

标签 ios swift uitableview uinavigationitem

简而言之:在 Swift/iOS 中,当退出 UITableViewController 的“编辑”模式时,“完成”(以前是“编辑”)导航栏按钮何时触发?当用户按下“完成”时,我想在我的 UINavigationItem 栏中启用一个“+”按钮,这样用户就可以通过迁移到另一个 View Controller 来再次添加行。

更长:当 UITableViewController 显示在 UINavigationItem 导航栏下方时,有一个“编辑”按钮,单击它以启用删除和移动/拖动后变为“完成”。当通过在作为 UITableViewController 类的一部分生成的 viewDidLoad() 中取消注释代码启用此按钮时,效果很好:

self.navigationItem.leftBarButtonItem = self.editButtonItem

我的移动/拖动和删除工作正常,但我想在用户处于编辑状态时适本地禁用我的“+”按钮(addBarButton,用于导航到另一个 View Controller 以添加新行)模式。然后我想在用户点击“完成”(返回“编辑”)后重新启用 addBarButton。

看起来在 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) 期间禁用 addBarButton 是正确的。如果我正确阅读了 Apple 的文档,则当用户在导航栏中按下编辑时会触发此操作。我不知道当用户按下“完成”(以前标记为“编辑”的按钮)时会触发什么。如果我在带有 moveRowAt 的 func tableView 之后启用我的 addBarButton“+”按钮,这会在用户按下“完成”之前启用 addBarButton。

我引用的 Apple 文档位于: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html#//apple_ref/doc/uid/TP40007451-CH11-SW1

如果我遗漏了一些明显的东西,我深表歉意。谢谢

最佳答案

答案就在UIViewController editButtonItem 文档的描述中:

If one of the custom views of the navigationItem property is set to the returned object, the associated navigation bar displays an Edit button if isEditing is false and a Done button if isEditing is true. The default button action invokes the setEditing(_:animated:) method.

最后一句是关键。您应该覆盖 TableView Controller 子类中的 setEditing(_:animated:) 方法。请务必调用 super 实现,然后根据 Controller 是进入还是退出编辑模式执行您想要的任何自定义操作。

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if (editing) {
        // User tapped the Edit button, do what you need
    } else {
        // User tapped the Done button, do what you need
    }
}

关于ios - 在 UITableViewController 的 UINavigationItem 栏中从 "Done"更改后按下 "Edit"时触发什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39811761/

相关文章:

iphone - 将资源预加载到 NSURLCache 中

iOS/Xcode : Name button created in storyboard name so it can be disabled

swift - 如何在可解码模型中使用继承

ios - 是否可以增加 UITableView 分隔线的粗细?

iphone - 动态调整表格的高度

ios - 在 IOS 中切片和 reshape MLMultiArray

ios - 如何在 iPhone 中调试应用程序?

swift - 如何在 tvOS 中授权 OAuth2

ios - 应用内购买重定向到另一个 View Controller

ios - 如何在我输入 UITextField 时获取谷歌建议并将其放在 UITableView 上