Michael
Michael

Reputation: 811

Push to specific viewController from AppDelegate

My application hierarchy is looking like this:

tabBarController >> navigationControllers (multiple) >> viewControllers (multiple)

When a certain event occur in the appDelegate I want the push a specific viewController in one of the navigationControllers. For now I have this:

UINavigationController *myNavigationController = [[(UITabBarController*)self.window.rootViewController viewControllers] objectAtIndex:0];

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
StoreViewController *targetViewController = (StoreViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"StoreViewController"];

[myNavigationController pushViewController:targetViewController animated:YES];

This only works if the application is currently in the same navigationController as the targetViewController. I want it to work regardless of where you are in the application. Thanks!

Upvotes: 0

Views: 1594

Answers (1)

rdelmar
rdelmar

Reputation: 104082

You need to programmatically switch to tab 0 where that particular navigation controller is, before you do the push.

UITabBarController *tbc = (UITabBarController*)self.window.rootViewController;
tbc.selectedIndex = 0;
UINavigationController *myNavigationController = tbc.selctedViewController;
etc......

The above code assumes that you want to push targetViewController from a particular navigation controller (the one in tab 0), but if you don't care which navigation controller pushes it, then just delete the tbc.selectedIndex = 0 line. The following line will push from which ever navigation controller is currently the selected one. I'm not sure from your question, which you want.

Upvotes: 4

Related Questions