Search code examples
iosxcodeuinavigationcontrolleruistoryboardrootviewcontroller

Go back to original Root View Controller from a third tier Navigation Controller


I have some storyboards setup as below:

Storyboard A

--> Root Navigation Controller --> Container View Controller --> View Controller --> Home View Controller --> Storyboard B Reference

Storyboard B

--> Container View Controller --> Navigation Controller --> View Controller --> Storyboard C Reference

Storyboard C

--> Navigation Controller --> View Controller

The gist of it is, I load the app and the rootViewController is set. I then browse through the app and end up in Storyboard B which contains a button that takes me to Storyboard C. On the View Controller in Storyboard C there is a button that I want to take me back to the start of the app.

How, from the View Controller in Storyboard C, can I get back to the Home View Controller in Storyboard A?

Things I have tried:

[self.navigationController.navigationController popViewControllerAnimated:animated];

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"HomeScreen"];
[self presentViewController:viewController animated:animated completion:nil];

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"HomeScreen"];
[UIApplication sharedApplication].keyWindow.rootViewController = viewController;

The above 3 all go back to the right place but then the app crashes with EXC_I386_GPFLT.

I have also tried a few other things that didn't work. I know this is probably something very simple and i'm just having a bad day. Any suggestions are greatly appreciated right now.


Solution

  • I seem to have solved it by performing the popViewControllerAnimated: method on the main thread:

    [self.navigationController.navigationController performSelectorOnMainThread:@selector(popViewControllerAnimated:) withObject:nil waitUntilDone:nil];