【问题标题】:How to pop back to root ViewController from viewController that was used?如何从使用的 viewController 弹回根 ViewController?
【发布时间】:2017-02-26 06:21:52
【问题描述】:

我有一个以编程方式显示视图控制器的导航控制器。现在,如果出现错误,我正在尝试返回导航控制器的根视图控制器,但我似乎无法返回那里。

我正在使用的页面是这样呈现的:

let vc = self.storyboard!.instantiateViewController(withIdentifier: page)
self.show(vc, sender: self)

如果我尝试按时返回:

self.navigationController?.popViewController(animated: true)

我粗鲁地收到警告:

表达式类型 UIViewController 未使用

我不想在这里使用 segues。

【问题讨论】:

  • 你在哪里调用 popViewController,你在哪里实例化同一个 VC?
  • 检查下面的答案。如果有效,请接受
  • 即使是您取消警告的第一条评论也很有效。条件检查更好。谢谢

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案1】:

self.为了避免警告使用:

_ = self.navigationController?.popViewController(animated: true).

要弹回根视图控制器,请使用:-

if let self.navigationController = self.window?.rootViewController as? UINavigationController {
     navigationController.popToRootViewControllerAnimated(true)
}

【讨论】:

    【解决方案2】:

    实际上,这不是一个糟糕的警告。编辑器指示/突出显示您没有使用从导航堆栈中弹出的视图控制器。如果您不想使用弹出视图控制器,可以忽略它们

    以下是导航控制器为弹出操作提供的一些方法(功能)。他们从它的导航堆栈中返回可选的 UIViewController(实例),即弹出。

        open func popViewController(animated: Bool) -> UIViewController? // Returns the popped controller.
    
        open func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? // Pops view controllers until the one specified is on top. Returns the popped controllers.
    
        open func popToRootViewController(animated: Bool) -> [UIViewController]?
    

    这里是示例代码,如何使用它:

    if let navigation = self.window?.rootViewController as? UINavigationController {
         navigation.popToRootViewControllerAnimated(true)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多