12

From Home view - my RootViewController - I open up 2 ViewControllers one after another as user progresses in navigation hierarchy like so:

1) SecondViewController is pushed by button connected in my Storyboard

2) ThirdViewController is presented modally

[self performSegueWithIdentifier:@"NextViewController" sender:nil];

So, the picture is: RootViewController -> SecondViewController -> ThirdViewController

Now in my ThirdViewController I want to have a button to go back 2 times to my RootViewController, i.e. go home. But this does not work:

[self.navigationController popToRootViewControllerAnimated:YES]; 

Only this guy goes back once to SecondViewController

[self.navigationController popViewControllerAnimated:YES];

How can I remove both modal and pushed view controllers at the same time?

Steph Sharp
  • 10,659
  • 4
  • 42
  • 79
Vad
  • 3,418
  • 8
  • 40
  • 73
  • I think what you want to do is in your thirdViewController: [self.presentingViewController dismissViewControllerAnimated:NO]; [self.presentingViewController popToRootViewControllerAnimated:YES];, excuse the poor typing I'm on a mobile – powerj1984 May 29 '13 at 23:52
  • possible duplicate of [Return to root view in IOS](http://stackoverflow.com/questions/9035320/return-to-root-view-in-ios) – jww Oct 05 '14 at 02:37

3 Answers3

21

I had a similar situation, where I had a number of view controllers pushed onto the navigation controller stack, and then the last view was presented modally. On the modal screen, I have a Cancel button that goes back to the root view controller.

In the modal view controller, I have an action that is triggered when the Cancel button is tapped:

- (IBAction)cancel:(id)sender
{
    [self.delegate modalViewControllerDidCancel];
}

In the header of this modal view controller, I declare a protocol:

@protocol ModalViewControllerDelegate
- (void)modalViewControllerDidCancel;
@end

And then the last view controller in the navigation stack (the one that presented the modal view) should implement the ModalViewControllerDelegate protocol:

- (void)modalViewControllerDidCancel
{
    [self dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

This method above is the important part. It gets the presenting view controller to dismiss the modal view, and then it pops back to the root view controller. Note that I pass NO to dismissViewControllerAnimated: and YES to popToRootViewControllerAnimated: to get a smoother animation from modal view to root view.

Steph Sharp
  • 10,659
  • 4
  • 42
  • 79
  • I am testing your solution. Delegate way is more complicated but maybe worth trying. – Vad May 31 '13 at 01:02
  • Your answer helped me. I only had to do it through post notification instead of delegate – Vad May 31 '13 at 01:28
  • And lastly the first line was [self dismissModalViewControllerAnimated:NO]; – Vad May 31 '13 at 01:30
  • @Vad I'm glad you got it working. The dismissModalViewControllerAnimated: method has been deprecated in iOS 6.0. The recommended method to use now is dismissViewControllerAnimated:completion:, [see doco here](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/doc/uid/TP40006926-CH3-SW30). – Steph Sharp May 31 '13 at 02:06
  • Thx, I spend a lot of time to get run a simular problem. Nothing worked !! But this simple solution solved the Problem. – feldeOne Sep 13 '16 at 07:50
4

I had the same requirement but was using custom segues between the view controllers. I came across with the concept of "Unwind Segue" which I think came with iOS6. If you are targeting iOS6 and above these links might help: What are Unwind segues for and how do you use them? http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards Thanks.

Community
  • 1
  • 1
cfc16
  • 61
  • 4
0

Assuming your AppDelegate is called AppDelegate, then you can do the following which will reset the rootviewcontroller for the app window as the view RootViewController

AppDelegate *appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
RootViewController *rootView = [[RootViewController alloc] init];
[appDel.window setRootViewController:rootView];
Adam Richardson
  • 2,448
  • 1
  • 25
  • 30