4

I want to go back two levels on my view controller stack. I have three segues in this order: Show, Show, Present Modally. There is a navigation controller in use. From my 4th view I want to go back to the 2nd view. I have tried using self.presentingViewController?.presentingViewController?.navigationController?.popViewControllerAnimated(false);

and

self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(false, completion: nil);

The second one works only if the 2nd and 3rd segues as 'Present Modally'. How can I get them to work with a dismiss and a pop?

Ricca
  • 311
  • 4
  • 21

4 Answers4

5

Try dismissing the presented view controller before popping the other two:

func dismissThenDoublePop() {

    // Get the presenting/previous view
    let previousView = self.presentingViewController as UINavigationController

    // Dismiss the current view controller then pop the others
    // upon completion
    self.dismissViewControllerAnimated(true, completion:  {

        // Get an array of the current view controllers on your nav stack
        let viewControllers: [UIViewController] = previousView.viewControllers as [UIViewController];

        // Then either pop two view controllers, i.e. pop
        // to viewControllers[viewControllers.count - 2], or
        // pop to the second view controller in the nav stack,
        // i.e. viewControllers[1]. (In this case I've used the
        // first option.)
        self.navigationController!.popToViewController(viewControllers[viewControllers.count - 2], animated: true);

    });
}
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • It gets to the line that starts with `let viewControllers:...` and crashes with "fatal error: unexpectedly found nil while unwrapping an Optional value" – Ricca Dec 17 '14 at 23:10
  • @Ricca Yep, you're right navigation controller is nil in the presented view controller. Looking into an alternative now. – Lyndsey Scott Dec 18 '14 at 05:39
  • @Ricca OK, I've updated my answer to get the previous view as a UINavigationController before the dismissView block. I've tested this code and it works for me. – Lyndsey Scott Dec 18 '14 at 05:49
  • @LyndseyScott I Tried in this way to move to the home screen. Could you please check whether it is correct or not? let previousView = self.presentingViewController as! UINavigationController self.dismiss(animated: true, completion: { // Get an array of the current view controllers on your nav stack let viewControllers: [UIViewController] = previousView.viewControllers as [UIViewController]; previousView.popToViewController(viewControllers[0], animated: true); }); – suji Oct 09 '17 at 10:28
3

You can use this technique

https://stackoverflow.com/a/15839298/1153100

Simple and clean(Unwind segues)

@IBAction func unwindAction(segue: UIStoryboardSegue) {
}
Community
  • 1
  • 1
iEmad
  • 625
  • 1
  • 7
  • 20
-1

You use like this one.

self.navigationController?.popViewControllerAnimated(true)
Nurdin
  • 23,382
  • 43
  • 130
  • 308
-1

Just call dismissViewControllerAnimated.It will automatically dismiss all view controllers including presented model viewcontroller.

Objective - C

[self.navigationController dismissViewControllerAnimated:YES completion:nil]; 

Swift

self.navigationController?.dismissViewControllerAnimated(false, completion: nil);
Rajeev Udayan
  • 310
  • 2
  • 4