ios: Accessing a navigation controller from app delegate

James Harpe picture James Harpe · Jan 2, 2013 · Viewed 31.1k times · Source

I have an app that receives push notifications. In didReceiveRemoteNotifications, I would like to make the app show a particular view controller in the app's navigation controller (which happens to be the root view controller). What is the best way to make this happen? Can I get a reference to the navigation controller in the app delegate?

EDIT: Here is the code I'm trying to use right now. It appears to use the correct navigation controller, but it doesn't display the view controller at all, just a blank screen:

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        EventDetailViewController *destCon = [storyboard instantiateViewControllerWithIdentifier:@"EventDetailViewController"];
        destCon.event=notifyEvent;
        UINavigationController *navController =(UINavigationController *) self.window.rootViewController;
        [navController pushViewController:destCon animated:YES];

Here is what I'm seeing:

enter image description here

Answer

rdelmar picture rdelmar · Jan 2, 2013

If your navigation controller is the root view controller of the window, then you can just use

(UINavigationController *)self.window.rootViewController

from the app delegate to access the one you created in the storyboard.