【问题标题】:self.navigationController?.popToRootViewController(animated: true) doesn't dismiss any presented view controllers [closed]self.navigationController?.popToRootViewController(animated: true) 不会关闭任何呈现的视图控制器 [关闭]
【发布时间】:2020-11-10 18:45:57
【问题描述】:

我正在开发一个在 Xcode 中使用 Swift 和 Storyboard 的应用程序。 2 个视图控制器相互叠加,我想通过按下按钮来关闭它们。我已经在 StackOverflow 上搜索了这里的答案,我找到了self.navigationController?.popToRootViewController(animated: true),但它没有用。您对为什么这行不通有什么想法吗?

解决方案:

在这种情况下起作用的只是使用另一行代码。我不知道有什么不同,但它以某种方式起作用:

self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)

【问题讨论】:

  • 你确定 self.navigationController 不是 nil 吗?
  • 如何检查?
  • @FrederikBrammer 使用调试器?

标签: ios swift xcode uiviewcontroller uinavigationcontroller


【解决方案1】:

检查您的 NavigationController 是否为零 ...

if let nav = self.navigationController {
     nav.popToRootViewController(animated: true)
} else {
   print("nav is nil")
}

【讨论】:

  • 它说导航为零,我该如何解决?
  • 现在你需要检查为什么会这样......你在展示这个控制器吗?
  • Pop 用于推送......而对于呈现的控制器,我们已经解雇......
  • 如果有帮助就接受...谢谢...愉快的编码 =)
  • 我只是使用了一个不同的命令,self.view.window!.rootViewController?.dismiss(animated: true, completion: nil) 为我工作。我不知道有什么区别,但它以某种方式起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多