1

I'm a bit puzzled by the following behavior in iOS (target is iPad with iOS 9.3 in Xcode 7.3.1). I have the following hierarchy:

Storyboard1
|
| initial
|
A (UIViewController)
|
| UIStoryboardSegue Present Modally
|
B (UINavigationController)
|
| root
|
C (UIViewController)
|
| presentViewController
|
Storyboard2
|
| initial
|
D (UIViewController)
|
| UIStoryboardSegue Present Modally
|
E (UIViewController) here

When E completes, I want to go back to A, so I thought about using self.navigationController?.dismissViewControllerAnimated(true, nil) in C, which I call from E by keeping a weak reference to C, but that brings me back to C and not A. If I want to go back to A I have to issue the same dismiss command twice. What am I missing?

Wolfy
  • 1,445
  • 1
  • 14
  • 28
  • how about use unwind segue? E to A this link will help you http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them – Cruz May 20 '16 at 14:25
  • @SwiftyCruz great suggestion! Make it an answer, and I'll accept it. :) – Wolfy May 20 '16 at 17:48

2 Answers2

1

Wolfy,

Simply call,

UIApplication.sharedApplication().keyWindow?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)

How it works

UIApplication.sharedApplication().keyWindow?.rootViewController will return `A (UIViewController)` 

since A ViewController has modally presented B (UINavigationController) and all other viewControllers are loaded in this navigation stack dismissing B (UINavigationController) will unload all the viewControllers for you :)

Happy coding :)

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
1

To dismiss multiple modals you can do this :

A.dismissViewControllerAnimated(true,nil)
stefos
  • 1,235
  • 1
  • 10
  • 18