Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: Accessing a navigation controller from app delegate

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

like image 545
James Harpe Avatar asked Jan 02 '13 18:01

James Harpe


People also ask

What is navigation delegate?

Overview. Use a navigation controller delegate (a custom object that implements this protocol) to modify behavior when a view controller is pushed or popped from the navigation stack of a UINavigationController object.

What is iOS app delegate?

The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

How do I get navigation controller from storyboard?

Under the View menu, select Utilities→Show Object Library. In the Object Library, find the Navigation Controller object (see Figure 4-7) and drag and drop it into the storyboard, to the left side of your existing view controller (Figure 4-6). Now you will see something similar to Figure 4-8.


1 Answers

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.

like image 155
rdelmar Avatar answered Sep 22 '22 06:09

rdelmar