Knob pop to root view from modal

I am trying to start the root view controller with 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 modal. How can I return to the root view controller in this situation?

Thanks in advance.

+15
source share
6 answers

You can check that the current controller is presented, if it is presented, then release it and go to rootViewController another wise thing by going directly to rootViewController

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

Result:

The code

Suppose your Modal View has a ViewController linked below.

Basically, to hide your view, which appears as Modal , you use the dismiss(animated: Bool) method from your ViewController instance.

And for representations represented as Pushed , you can use these methods from your navigationController property, for example: popToRootViewController(animated: Bool) , popViewController(animated:Bool)

 class ModalViewController: UIViewController { @IBAction func backButtonTouched(_ sender: AnyObject) { let navigationController = self.presentingViewController as? UINavigationController self.dismiss(animated: true) { let _ = navigationController?.popToRootViewController(animated: true) } } } 
+10
source

You introduced a ViewController, so you need to decline it.

To disable ViewController in Swift, use this:

 self.dismiss(animated: true, completion: nil) 
+4
source

You can do something like this:

Objective-C:

 [(UINavigationController *)self.presentingViewController popToRootViewControllerAnimated:NO]; [self dismissViewControllerAnimated:YES completion:nil]; 

Check the answer here for reference:

ios: how to reject a modal view controller and then pull out the pushed view controller

0
source

What about usage notice?

your error message only mod message

and then your navigation controller, pop into rootViewController

0
source

If you do not want the animation to occur, that is, you want the user to see the root view controller after rejecting the modal view:

 CATransaction.begin() CATransaction.setCompletionBlock { self.dismiss(animated: true, completion: nil) } self.navigationController?.popViewController(animated: false) CATransaction.commit() 

Help from: Completion Block for popViewController

Above will work to pop one view controller. If you need to pop out a few, popToRootViewController will not work (unbalanced call due to animation).

In this case, use the following (manual) method to remove them:

 guard let navigationController = self.navigationController else { return } var navigationViewControllers = navigationController.viewControllers navigationViewControllers.removeLast(navigationViewControllers.count - 1) self.navigationController?.viewControllers = navigationViewControllers 
0
source

All Articles