ios 如何从 AppDelegate 获取 navController。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10039856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:44:28  来源:igfitidea点击:

How to get navController from AppDelegate.

iphoneobjective-ciosuinavigationcontrolleruiapplicationdelegate

提问by ramo

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.

我想知道,如何从AppDelegate = [[UIApplication sharedApplication] delegate]iPhone 编程中获取 navController 。例如,在我们引用 AppDelegate 的其他 viewController 中。

In the applicationDelegate.h we have:

在 applicationDelegate.h 中,我们有:

UINavigationController *navController;

And the following in applicationDelegate.m

以及 applicationDelegate.m 中的以下内容

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

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

Is there anyway to get the navController from the mainWindow:

无论如何从mainWindow获取navController:

UIWindow *mainWindow = [appDelegate window];

回答by Lucien

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

如果这个其他 UIViewController 包含在 UINavigationController 中,你可以简单地调用:

UINavigationController *navController = self.navigationController;

from the UIViewController.

来自 UIViewController。

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

否则,您可以将 UINavigationController 设置为 AppDelegate 中的一个属性。

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

Then access appDelegate.navController.

然后访问appDelegate.navController.

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

或者,您可以将 UINavigationController 设置为窗口的 rootViewController:

[window setRootViewController:navController];

And call from anywhere:

并从任何地方调用:

UINavigationController *navController = window.rootViewController;

回答by Otium

You can make the navController a property

您可以使 navController 成为一个属性

@property (nonatomic,strong) UINavigationController *navController;

Then just access it from your appdelegate

然后只需从您的 appdelegate 访问它

appDelegate.Controller

回答by tangqiaoboy

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

您可以将navController设为委托类的属性。示例如下:

In applicationDelegate.h

在 applicationDelegate.h

@property (retain, nonatomic) UINavigationController *navController;

In applicationDelegate.m

在 applicationDelegate.m 中

@synthesize navController;

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

那么你可以使用下面的代码来获取其他类中的navController(假设你的委托类是MyApplicationDelegate):

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

回答by Remco Nijhuis

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.

当您将宏放在源代码的顶部或导入源代码的 .h 头文件中时,您就可以开始使用 mainNavController,就好像它是一个局部变量一样。

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.

您几乎可以在任何地方使用此代码,如果您在类中工作并且无法直接访问 ViewController,这将非常方便。

回答by Kuldeep

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 对象访问导航控制器。

[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

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