简而言之:在 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。
如果我遗漏了一些明显的东西,我深表歉意。谢谢
最佳答案
答案就在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 ifisEditing
is false and a Done button ifisEditing
is true. The default button action invokes thesetEditing(_: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/