从模态弹出到根视图控制器 [英] Pop to root view controller from modal

查看:94
本文介绍了从模态弹出到根视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码弹出到根视图控制器:

I am trying to pop to the root view controller using the following code:

self.navigationController!.popToRootViewController(animated: true)

这通常有效,但在当前尝试使用此代码时出错视图是一种模态。在这种情况下如何弹回到根视图控制器?

This usually works, but I get an error when trying to use this code when the current view is a modal. How do I go about popping back to the root view controller in this situation?

提前致谢。

推荐答案

你可以检查当前控制器是否出现,如果出现然后解除它并转到 rootViewController 其他明智的去直接 rootViewController

You can check that current controller is presented, if it is presented then dismiss it and the go to the rootViewController other wise go directly the rootViewController

if self.presentingViewController != nil {
    self.dismiss(animated: false, completion: { 
       self.navigationController!.popToRootViewController(animated: true)
    })
}
else {
    self.navigationController!.popToRootViewController(animated: true)
}

这篇关于从模态弹出到根视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆