How to get navController from AppDelegate.

ramo picture ramo · Apr 6, 2012 · Viewed 29.2k times · Source

I am wondering, that how to get navController from AppDelegate = [[UIApplication sharedApplication] delegate] in the iPhone programming. e.g., in other viewController where we reference to the AppDelegate.

In the applicationDelegate.h we have:

UINavigationController *navController;

And the following in applicationDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

   [window addSubview: navController.view];
   [window makeKeyAndVisible];
}

Is there anyway to get the navController from the mainWindow:

UIWindow *mainWindow = [appDelegate window];

Answer

Lucien picture Lucien · Apr 6, 2012

If this other UIViewController is contained in the UINavigationController, you can simply call:

UINavigationController *navController = self.navigationController;

from the UIViewController.

Otherwise, you can set UINavigationController as a property in the AppDelegate.

// AppDelegate.h
@property (nonatomic, strong) UINavigationController *navController;

Then access appDelegate.navController.

Or, you can set the UINavigationController as window's rootViewController:

[window setRootViewController:navController];

And call from anywhere:

UINavigationController *navController = window.rootViewController;