简体   繁体   English

如何弹出除一个以外的所有视图控制器?

[英]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.

相关问题 如何关闭除一个视图控制器以外的所有视图控制器并弹出到 Swift 中的指定视图 controller? - How to dismiss all but one view controllers and pop to specified view controller in Swift? 快速弹出所有视图控制器 - Swift pop all view controllers iOS:删除堆栈中所有视图控制器(活动中的一个除外) - iOS: Delete all view controllers on stack except active one 如何快速将视图控制器弹出到以前的视图控制器之一? - How to pop view controller to one of the previous view controllers in swift? 如何“弹出”导航控制器及其所有视图控制器 - How to 'pop' a navigation controller and all of its view controllers 如何在 UINavigationController 堆栈中“弹出”多个视图控制器? - How to “pop” several View controllers in UINavigationController Stack? 如何在 iOS 13 中查看和弹出视图控制器 - how to peek and pop view controllers in iOS 13 除模态视图控制器外的所有视图控制器中的纵向方向 - Portrait orientation in all view controllers except in modal view controller 如何在所有视图控制器上设置一个标签来设置分数? - How to make one label to set score on all view controllers? 如何在所有视图控制器中调用一个方法? - How can I call the one method in all the view controllers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM