7

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];
Irfan
  • 4,301
  • 6
  • 29
  • 46
ramo
  • 609
  • 2
  • 8
  • 14
  • 1
    Why don't you make the nav controller a property of your app delegate? –  Apr 06 '12 at 06:25
  • I need to get the information about the navController only by the mainWindow (UIWindow *mainWindow = [appDelegate window]), is taht possible. – ramo Apr 06 '12 at 06:41
  • I suppose no. But why can't you use the app delegate, really? –  Apr 06 '12 at 06:54
  • looks like i need to use the Obj-C Runtime Library like http://stackoverflow.com/questions/754824/get-an-object-attributes-list-in-objective-c – ramo Apr 06 '12 at 07:18

5 Answers5

18

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;
Lucien
  • 8,263
  • 4
  • 30
  • 30
  • Very instructive! Thank you! One question though: if you're going to access the navController through `window`, don't you have to get access through the UIApplication? Is there perhaps a shorter way? – JohnK Jun 25 '13 at 18:41
  • You can get to the window from any UIView with the `window` property. – Lucien Jun 29 '13 at 13:48
2

You can make the navController a property

@property (nonatomic,strong) UINavigationController *navController;

Then just access it from your appdelegate

appDelegate.Controller
Otium
  • 1,098
  • 8
  • 20
1

You can make the navController as a property of your delegate class. sample below:

In applicationDelegate.h

@property (retain, nonatomic) UINavigationController *navController;

In applicationDelegate.m

@synthesize navController;

then you can use the following code to get the navController in other classes (Assume your delegate class is MyApplicationDelegate):

appDelegate = (MyApplicationDelegate*)[[UIApplication sharedApplication] delegate];
UINavigationController *navController = appDeleagte.navController
tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32
1

No extra properties needed, available almost anywhere in your application using this macro definition:

#define mainNavController (((AppDelegate*)[[UIApplication sharedApplication] delegate]).navController)

When you put the macro at the top of your source or in a .h header file that you import into your source, then you can start using mainNavController as if it were a local variable.

For example:

[mainNavController pushViewController:myViewController animated:YES];

Or without the macro, directly in code:

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.navController; // do something with the navController

You can use this code almost anywhere, which is handy if you're working inside a class and you can't access a ViewController directly.

Remco Nijhuis
  • 134
  • 1
  • 3
-1

If you are beginer and learner, the navigation controller is shared in whole application which will just prepare the "stack" of your app's viewcontrollers, so you can access the navigationcontroller in any viewcontroller(Only if that controller has been pushed) through out the app. When you push any controller it will added to the "stack" of navigation controller.

You can access the navigation controller with the self object of that viewcontroller itself.

[self.navigationController pushViewController:detail animated:YES];

Go through with the link will give complete knowledge of navigation anatomy.

http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html

Kuldeep
  • 2,589
  • 1
  • 18
  • 28