0

Storyboard

In the app I am building, I wish to be able to move from the "last" view controller to the view controller used when the app launches. What is the best way to do this? I much like the functionality of:

[self.navigationController popToViewController:originalVC animated:YES];

However it does not appear to work in this case as the original VC is not contained within the Navigation Controller. When I use:

[self presentViewController:launchVC animated:YES completion:nil];

the app just shows the black screen of death.

Thanks.

Jeremy H
  • 452
  • 7
  • 20

2 Answers2

1
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *originalViewController = viewControllers[0];
[self.navigationController setViewControllers:@[originalViewController] animated:YES];

UPDATE

Sorry, I misunderstood your question. The correct answer is:

UIViewController *originalViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"originalViewControllerStoryboardId"];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = originalViewController;
Artem Stepanenko
  • 3,423
  • 6
  • 29
  • 51
  • Work great, thanks! However the transition is a little abrupt. Happen to have a cool trick for that as well? – Jeremy H Jul 03 '14 at 16:04
  • @JeremyHerrero, If it works, what about accepting the answer? – Artem Stepanenko Jul 08 '14 at 16:02
  • @JeremyHerrero, can you describe problem in more details? – Artem Stepanenko Jul 08 '14 at 16:02
  • Sorry about that. Accepting now. The only thing is when I execute that line of code, the other VC appears without any type of transition or animation. I would very much like for it to transition like the back button does on a push segue if possible. – Jeremy H Jul 08 '14 at 16:24
1

You can do this with an unwind segue. Just create a method in your initial controller that is an IBAction with an argument that's a UIStoryboardSegue, and then connect up your last controller (or a UI element like a button or cell) to the exit icon, and choose that method you wrote in the initial controller. This will take you back to the same instance of the initial controller that you started with, and all the controllers in between will be deallocated (if you don't have any strong pointer to them in the initial controller).

rdelmar
  • 103,982
  • 12
  • 207
  • 218