Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get navController from AppDelegate.

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];
like image 555
ramo Avatar asked Apr 06 '12 06:04

ramo


2 Answers

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;
like image 125
Lucien Avatar answered Sep 20 '22 23:09

Lucien


You can make the navController a property

@property (nonatomic,strong) UINavigationController *navController;

Then just access it from your appdelegate

appDelegate.Controller
like image 35
Otium Avatar answered Sep 19 '22 23:09

Otium