How to get navController from AppDelegate.

I am wondering how to get navController from AppDelegate = [[UIApplication sharedApplication] delegate] in iPhone programming. for example, in another viewController, where we refer to AppDelegate.

In applicationDelegate.h we have:

 UINavigationController *navController; 

And the following in applicationDelegate.m

 - (void)applicationDidFinishLaunching:(UIApplication *)application { [window addSubview: navController.view]; [window makeKeyAndVisible]; } 

In any case, to get navController from mainWindow:

 UIWindow *mainWindow = [appDelegate window]; 
+6
source share
5 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 the UINavigationController as a property in AppDelegate.

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

Then select appDelegate.navController .

Or you can set the UINavigationController as the rootViewController window:

 [window setRootViewController:navController]; 

And call from anywhere:

 UINavigationController *navController = window.rootViewController; 
+17
source

You can make navController a property

 @property (nonatomic,strong) UINavigationController *navController; 

Then just open it from the appdelegate application

 appDelegate.Controller 
+2
source

You can make 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 navController in other classes (suppose your delegate class is MyApplicationDelegate ):

 appDelegate = (MyApplicationDelegate*)[[UIApplication sharedApplication] delegate]; UINavigationController *navController = appDeleagte.navController 
+1
source

No additional properties are available anywhere in the application using this macro definition:

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

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

For instance:

 [mainNavController pushViewController:myViewController animated:YES]; 

Or without a macro, directly in the code:

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

You can use this code almost anywhere, which is convenient if you are working inside a class and you cannot directly access the ViewController.

+1
source

If you are a beginner and student, the navigation controller is used throughout the application, which will simply prepare the "stack" of your application view managers so that you can access the navigation manager in any view manager (only if this controller has been clicked) through from the application. When you press a controller, it is added to the "stack" of the navigation controller.

You can access the navigation controller with the self-object of this view manager itself.

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

Follow the link for a complete knowledge of the anatomy of navigation.

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

-1
source

Source: https://habr.com/ru/post/912565/


All Articles