Return to root view controller from multiple modal presentations or segues (Xcode for iOS)

If you are returning to the root view controller from a series of modally presented view controllers then the following code will work in iOS6

[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];

In iOS5 it would be

[self.view.window.rootViewController dismissModalViewControllerAnimated:YES];

If you want to dismiss all modally presented view controllers from the App Delegate, for example from

- (void)applicationWillResignActive:(UIApplication *)application

then you'd write


[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];


Or for iOS5

[self.window.rootViewController dismissModalViewControllerAnimated:NO];

Comments

  1. nice blog thanks :)

    ReplyDelete
  2. can we jump to one presentmodeviewcontoller to Another viewContoller

    ReplyDelete
  3. Thanks for your comments. I haven't tried this or touched this code in a while, but I guess one way would be to use a container view controller setup - e.g. http://sketchytech.blogspot.co.uk/2012/09/container-view-controllers-parent-view.html - then when the willMoveToParent method is called (i.e. when a transition is made to the second "contained" view controller) you could dismiss the first controller you added to the stack of the first "contained" view controller (which would dismiss all the rest on top).

    Does that help?

    ReplyDelete
  4. Thanks so much works great in iOS7. Unfortunately this is not working for me in iOS8.

    I have a ipad splitviewcontroller and I hide that with a modal disclaimer, accept the disclaimer and I push to modal login. In iOS8 the login view is dismissed but the disclaimer view is not.

    Ideas?

    ReplyDelete
    Replies
    1. Pleasure. I've tested this by hooking up three view controllers starting with a split view controller, modally presenting a second, doing a push presentation to the third. Then there's a button to dismiss all and return to split vc. All of this is done using the storyboard and I can't find a combination of presentations that break the above code in the way you describe.

      Sorry that I don't have anything additional to offer at present.

      Delete
  5. THANKS!!! I have been trying to figure out how to do this for over a couple hours now, and it turns out it is so simple

    ReplyDelete
    Replies
    1. No problem, I appreciate the feedback. Thanks.

      Delete

Post a Comment