【问题标题】:Set bar button item as back button on navigation controller将栏按钮项设置为导航控制器上的后退按钮
【发布时间】:2017-09-16 17:23:56
【问题描述】:

我有 3 个视图控制器,第一个视图控制器嵌入在导航控制器中。在我的第二个视图控制器上,我隐藏了导航栏,并且我有一个自定义的后退按钮,我想用它来返回上一页。

这是我在工具栏上的后退按钮。

@IBOutlet var backButton: UIBarButtonItem!

这是返回按钮的功能

@IBAction func exitEditor(_ sender: Any) {
    navigationController?.navigationBar.popItem(animated: true)
}

当我测试它时,我得到了这个错误:

'NSInternalInconsistencyException',原因:'不能直接在控制器管理的 UINavigationBar 上调用 popNavigationItemAnimated。'

【问题讨论】:

  • 只需将此行 - navigationController?.navigationBar.popItem(animated: true) 替换为 - self.navigationController?.popViewController(animated: true) 。你的代码会像魅力一样运行;)
  • 但是,在这里,我的 ViewController 被弹出了。但是,NavigationBar 仍然是最后一页。例如:从第 3 页开始,我正在调用您的代码,第 2 页可见,但导航栏仍将第 3 页显示为标题
  • @AmirKhan,你能指导我吗?
  • @McDonal_11 嗨,你解决了这个错误吗?
  • 不,我不能。你能指导我吗?

标签: ios swift swift3


【解决方案1】:
@IBAction func exitEditor(_ sender: Any) {
    self.navigationController?.popViewController(animated: true)
}

这是从导航堆栈中弹出当前 viewController 的方式。

【讨论】:

  • 但是,在这里,我的 ViewController 被弹出了。但是,NavigationBar 仍然是最后一页。例如:从第 3 页开始,我正在调用您的代码,第 2 页可见,但导航栏仍将第 3 页显示为标题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多