[英]How to pop all view controllers except one?
Let I have FirstViewController
. 让我有
FirstViewController
。 From FirstViewController
, user opens modally UINavigationController(rootViewController: SecondViewController())
. 用户从
FirstViewController
模态打开UINavigationController(rootViewController: SecondViewController())
。 From SecondViewController
, user can go further and further using pushViewController
method. 在
SecondViewController
,用户可以使用pushViewController
方法走得更远。 During this travel, user comes to FifthViewController
, where close button exists as a leftBarButtonItem
. 在此旅行期间,用户进入
FifthViewController
,其中关闭按钮作为leftBarButtonItem
存在。 When it is pressed, I want to show FirstViewController
, not SecondViewController
. 当它被按下时,我想显示
FirstViewController
,而不是SecondViewController
。 How to achieve this using best practice? 如何使用最佳实践来实现这一目标?
My idea: From FifthViewController
, it is possible to use popToRootViewController
method. 我的想法:从
FifthViewController
,可以使用popToRootViewController
方法。 It will show SecondViewController
. 它将显示
SecondViewController
。 In SecondViewController, I need to somehow handle this (I don't know how) and dismiss it. 在SecondViewController中,我需要以某种方式处理它(我不知道如何)并将其关闭。 But I don't think that this method is correct.
但是我认为这种方法是不正确的。 Also, this method is long, because SecondViewController is shown (that can be possibly avoided).
而且,此方法很长,因为显示了SecondViewController(可以避免)。 So, what is the best way?
那么,最好的方法是什么?
All you need to do is call self.dismiss
on any of the children of navigation controller. 您需要做的就是在导航控制器的
self.dismiss
上调用self.dismiss
。 This will remove all of your pushed view controllers in UINavigationController and dismiss the UINavigationController itself. 这将删除UINavigationController中所有推入的视图控制器,并关闭UINavigationController本身。
You can find the navigation stack in navigationController?.viewControllers
. 您可以在
navigationController?.viewControllers
找到导航堆栈。 Take the viewController you are looking for and use 使用您要查找的viewController并使用
navigationController?.popToViewController(someViewController, animated: true)
to go back. 回去。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.