ios Swift - 如何关闭所有视图控制器以返回 root
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47322379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - How to dismiss all of view controllers to go back to root
提问by Byoth
I want a my app can go to a first view controller when every time users want it.
我希望我的应用程序可以在每次用户需要时转到第一个视图控制器。
So I want to create a function to dismiss all the view controllers, regardless of whether it is pushed in navigation controllers or presented modally or opened anything methods.
所以我想创建一个函数来关闭所有视图控制器,无论它是在导航控制器中推送还是以模态呈现或打开任何方法。
I tried various ways, but I failed to dismiss all the view controllers certainly. Is there an easy way?
我尝试了各种方法,但我肯定没有关闭所有的视图控制器。有没有简单的方法?
回答by Agent Smith
Try This :
尝试这个 :
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
it should dismiss all view controllers above the root view controller.
它应该关闭根视图控制器上方的所有视图控制器。
If that doesn't work than you can manually do that by running a while loop like this.
如果这不起作用,您可以通过运行像这样的 while 循环来手动执行此操作。
func dismissViewControllers() {
guard let vc = self.presentingViewController else { return }
while (vc.presentingViewController != nil) {
vc.dismiss(animated: true, completion: nil)
}
}
It would dismiss all viewControllers until it has a presentingController.
它会关闭所有 viewController 直到它有一个presentingController。
Edit :if you want to dismiss/pop pushed ViewControllers you can use
编辑:如果您想关闭/弹出推送的 ViewControllers,您可以使用
self.navigationController?.popToRootViewController(animated: true)
Hope it helps.
希望能帮助到你。
回答by Sandip Gill
If you are using Navigation you can use first one or if you are presenting modally you can second one:
如果您使用导航,您可以使用第一个,或者如果您以模态呈现,您可以使用第二个:
For Navigation
导航用
self.navigationController?.popToRootViewController(animated: true)
For Presenting modally
用于模态呈现
self.view.window!.rootViewController?.dismissViewControllerAnimated(false, completion: nil)
回答by Syed Qamar Abbas
Simply ask your rootViewController
to dismiss any ViewController
if presenting.
只需要求您在演示时rootViewController
忽略任何内容即可ViewController
。
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.window?.rootViewController?.dismiss(animated: true, completion: nil)
(appDelegate.window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: true)
}
回答by ap00724
Hello everyone here is the answer for Swift-4.
大家好,这里是Swift-4的答案。
To go back to root view controller, you can simply call a line of code and your work will be done.
要返回根视图控制器,您只需调用一行代码即可完成工作。
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
And if you have the splash screen and after that the login screen and you want to go to login screen you can simply append presentedviewcontrollerin the above code.
如果你有启动画面,然后是登录画面,并且你想进入登录画面,你可以简单地在上面的代码中附加presentedviewcontroller。
self.view.window?.rootViewController?.presentedViewController!.dismiss(animated: true, completion: nil)
回答by Faris Muhammed
Use this code for dismiss presented viewcontrollers and pop to navigation rootviewcontroller swift 4
使用此代码关闭呈现的视图控制器并弹出导航 rootviewcontroller swift 4
// MARK:- Dismiss and Pop ViewControllers
func dismissPopAllViewViewControllers() {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.window?.rootViewController?.dismiss(animated: true, completion: nil)
(appDelegate.window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: true)
}
}
回答by Shauket Sheikh
Pops all the view controllers on the stack except the root view controller and updates the display.
弹出堆栈上除根视图控制器之外的所有视图控制器并更新显示。
func popToRootViewController(animated: Bool)
But if you want to go to specific controller just use the below function.
但是如果你想转到特定的控制器,只需使用以下功能。
func popToViewController(UIViewController, animated: Bool)
Pops view controllers until the specified view controller is at the top of the navigation stack.
弹出视图控制器,直到指定的视图控制器位于导航堆栈的顶部。
回答by Srinath Shah
Create an Unwind Segue(You can find it at https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.htmlcopyright of Apple Inc.)
创建一个 Unwind Segue(你可以在https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html苹果公司的版权中找到它)
Unwind segues let you dismiss view controllers that have been presented. You create unwind segues in Interface Builder by linking a button or other suitable object to the Exit object of the current view controller. When the user taps the button or interacts with the appropriate object, UIKit searches the view controller hierarchy for an object capable of handling the unwind segue. It then dismisses the current view controller and any intermediate view controllers to reveal the target of the unwind segue.
Unwind segues 允许您关闭已呈现的视图控制器。通过将按钮或其他合适的对象链接到当前视图控制器的 Exit 对象,您可以在 Interface Builder 中创建 unwind segues。当用户点击按钮或与适当的对象交互时,UIKit 会在视图控制器层次结构中搜索能够处理 unwind segue 的对象。然后它解除当前视图控制器和任何中间视图控制器以显示展开转场的目标。
To create an unwind segue
创建放松转场
Choose the view controller that should appear onscreen at the end of an unwind segue.
Define an unwind action method on the view controller you chose.
选择应该在展开转场结束时出现在屏幕上的视图控制器。
在您选择的视图控制器上定义一个展开操作方法。
The Swift syntax for this method is as follows:
此方法的 Swift 语法如下:
@IBAction func myUnwindAction(unwindSegue: UIStoryboardSegue)
The Objective-C syntax for this method is as follows:
此方法的 Objective-C 语法如下:
- (IBAction)myUnwindAction:(UIStoryboardSegue*)unwindSegue
3. Navigate to the view controller that initiates the unwind action.
3. 导航到启动展开操作的视图控制器。
Control-click the button (or other object) that should initiate the unwind segue. This element should be in the view controller you want to dismiss.
Drag to the Exit object at the top of the view controller scene.
按住 Control 键单击应启动展开转场的按钮(或其他对象)。这个元素应该在你想要关闭的视图控制器中。
拖动到视图控制器场景顶部的 Exit 对象。
- Select your unwind action method from the relationship panel.
- 从关系面板中选择您的展开操作方法。
You must define an unwind action method in one of your view controllers before trying to create the corresponding unwind segue in Interface Builder. The presence of that method is required and tells Interface Builder that there is a valid target for the unwind segue.
在尝试在 Interface Builder 中创建相应的 unwind segue 之前,您必须在其中一个视图控制器中定义一个 unwind action 方法。该方法的存在是必需的,并告诉 Interface Builder 有一个有效的展开转场目标。
回答by Frostmourne
In case anyone looking for an Objective-C implementation of the question's answer,
如果有人正在寻找问题答案的 Objective-C 实现,
[self.view.window.rootViewController dismissViewControllerAnimated:true completion:nil];
回答by beshio
To achieve what you want, modify your navigation stack, then do popViewController.
要实现您想要的,请修改您的导航堆栈,然后执行 popViewController。
let allControllers = NSMutableArray(array: navigationController!.viewControllers)
let vcCount = allControllers.count
for _ in 0 ..< vcCount - 2 {
allControllers.removeObject(at: 1)
}
// now, allControllers[0] is root VC, allControllers[1] is presently displayed VC. write back to nav stack
navigationController!.setViewControllers(allControllers as [AnyObject] as! [UIViewController], animated: false)
// then pop root VC
navigationController!.popViewController(animated: true)
See thisfor the way to further manipulate the navigation stack. If your topmost VC is modal, dismiss it first before the code above.
见这对于进一步处理导航堆栈的方式。如果您最顶层的 VC 是模态的,请在上面的代码之前先关闭它。
回答by Srinath Shah
May be what you are looking for is unwind segue.
可能是您正在寻找的是 unwind segue。
Unwind segues give you a way to "unwind" the navigation stack back through push, modal, popover, and other types of segues. You use unwind segues to "go back" one or more steps in your navigation hierarchy.
Unwind segues 为您提供了一种通过推送、模态、弹出和其他类型的 segue 来“展开”导航堆栈的方法。您可以使用 unwind segue 来“返回”导航层次结构中的一个或多个步骤。
Link to documentation: https://developer.apple.com/library/archive/technotes/tn2298/_index.html
文档链接:https: //developer.apple.com/library/archive/technotes/tn2298/_index.html