Get storyboard-defined UINavigationController from AppDelegate

In storyboard, I have an introductory view controller which fades to a UINavigationController.

      |   |      |   |
   -> |   |  ->  |   |     
      |   |      |   |
     ViewCtrl   NavCtrl

I would like to get a reference to the navigation controller from the app delegate (like the @Guillaume answer does in this question).

Here's how I'm trying to access it:

UIApplication.sharedApplication.delegate.window.rootViewController.navigationController;

But the navigation controller is nil.

Any notion why the navController is nil or what I can do to get a reference to it?

Answers


UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];

UINavigationController *controller = (UINavigationController*)[mainStoryboard 
                    instantiateViewControllerWithIdentifier: @"<Controller ID>"];

You specify the controller identifier for your navigation controller in the attributes inspector of the navigation controller.


So Arsen's solution looks nice but I didnt know how to get the name of my storyboard lol. What worked for me was

UINavigationController *navigationController = (UINavigationController*)_window.rootViewController;
AVDashboardViewController *dashBoardViewController =
(AVDashboardViewController*)[navigationController.viewControllers  objectAtIndex:1];//since on index 0 I have my login screen and index 1 is the home screen

Getting the NavigationController's reference on the AppDelegate might seem like bad coding practice, but in my case I needed it because it is where the

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 

is located, which is one of the methods called when a Notification is received. I also struggled on how to update the navigation controller once I got the message, so maybe off topic but here is a free tip:

UIBarButtonItem *notiButton = [[UIBarButtonItem alloc] initWithTitle: @"Received push!" style: UIBarButtonItemStyleBordered target:self action:nil];

[[dashBoardViewController navigationItem] setLeftBarButtonItem:notiButton animated:YES];

[navigationController popToViewController:dashBoardViewController animated:YES];

Hope it helps!


Need Your Help

selecting some columns with grep

awk grep

I have a text file like this

J2ME API Hard to Find

api file java-me

Where is the API for J2ME? Why is it more difficult to find J2ME's API than its J2SE counterpart? Typing String and Java 1.6 will immediately present one with the String Javadoc for J2SE 6, but doi...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.