Swift - How to dismiss all of view controllers to

2020-05-13 09:23发布

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?

12条回答
贪生不怕死
2楼-- · 2020-05-13 10:12

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)
查看更多
欢心
3楼-- · 2020-05-13 10:12

Use this code for dismiss presented viewcontrollers and pop to navigation 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)
    }
}
查看更多
疯言疯语
4楼-- · 2020-05-13 10:12

May be what you are looking for is 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.

Link to documentation: https://developer.apple.com/library/archive/technotes/tn2298/_index.html

查看更多
够拽才男人
5楼-- · 2020-05-13 10:15

Create an Unwind Segue (You can find it at https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html copyright of Apple Inc.)

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.

To create an unwind segue

  1. Choose the view controller that should appear onscreen at the end of an unwind segue.

  2. Define an unwind action method on the view controller you chose.

The Swift syntax for this method is as follows:

@IBAction func myUnwindAction(unwindSegue: UIStoryboardSegue)

The Objective-C syntax for this method is as follows:

- (IBAction)myUnwindAction:(UIStoryboardSegue*)unwindSegue

3. Navigate to the view controller that initiates the unwind action.

  1. 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.

  2. Drag to the Exit object at the top of the view controller scene.

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/Art/segue_unwind_linking_2x.png

  1. 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.

查看更多
Melony?
6楼-- · 2020-05-13 10:17

In case anyone looking for an Objective-C implementation of the question's answer,

[self.view.window.rootViewController dismissViewControllerAnimated:true completion:nil];
查看更多
小情绪 Triste *
7楼-- · 2020-05-13 10:18

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.

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.

Edit : if you want to dismiss/pop pushed ViewControllers you can use

self.navigationController?.popToRootViewController(animated: true)

Hope it helps.

查看更多
登录 后发表回答