xcode 如何从 App Delegate 和/或其他视图控制器访问多个视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9691174/
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-09-14 23:38:20  来源:igfitidea点击:

How to access multiple View Controllers from App Delegate and/or other View Controllers?

iosxcodeuiviewcontrollerviewcontroller

提问by Richard Williams

I suspect this is a very simple answer or I'm on totally the wrong track. I need to be able to set variables and access fields etc in a view controller from either App Delegate or another view controller.

我怀疑这是一个非常简单的答案,或者我完全走错了路。我需要能够从 App Delegate 或另一个视图控制器在视图控制器中设置变量和访问字段等。

Previously I could do this from App Delegate to my first view controller by doing the following in 'didFinishLaunchingWithOptions':

以前,我可以通过在“didFinishLaunchingWithOptions”中执行以下操作,从 App Delegate 到我的第一个视图控制器执行此操作:

viewController = (ViewController *)self.window.rootViewController;

viewController = (ViewController *)self.window.rootViewController;

After that I could/can access methods within the view controller from App Delegate by doing the normal [viewController someMethod];.

之后,我可以/可以通过执行正常的 [viewController someMethod]; 从 App Delegate 访问视图控制器中的方法。

If I have multiple view controllers (currently have 3), how can I access the other ones from other locations? Incidentally I have found SOME explanations but all talk about nib/xibs combined with code. I don't have them, I have a storyboard and code (I'm new to app dev).

如果我有多个视图控制器(目前有 3 个),我如何从其他位置访问其他视图控制器?顺便说一句,我找到了一些解释,但都在谈论 nib/xib 与代码的结合。我没有它们,我有一个故事板和代码(我是应用程序开发的新手)。

Thanks!

谢谢!

回答by Rok Jarc

In your appDelegate you can simply declare properties that will hold references to this viewControllers.

在您的 appDelegate 中,您可以简单地声明将保存对此 viewController 的引用的属性。

For example:

例如:

in AppDelegate.h

在 AppDelegate.h 中

@property (nonatomic, strong) UIViewController *vc1;
@property (nonatomic, strong) UIViewController *vc2;

in AppDelegate.m

在 AppDelegate.m 中

@synthesize vc1,vc2;

Wherever you are creating this viewControllersyou can access your appDelegateand set it's properties to hold the right reference. Don't forget to include AppDelegate.hto this file.

无论您在哪里创建它,viewControllers您都可以访问您的appDelegate并设置它的属性以保存正确的引用。不要忘记包含AppDelegate.h到这个文件中。

UIViewController *someVC = [init view controller....];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.vc1 = someVC;

And from then on you can access this viewController as appDelegate's property.

从那时起,您可以将这个 viewController 作为 appDelegate 的属性访问。

EDIT: for storyBoarding it looks like this would be safe to do in - (void)viewDidLoad.

编辑:对于 storyBoarding,在- (void)viewDidLoad.

Or an even better approach would be (as Richard mentioned) to do this in - (id)initWithCoder:(NSCoder *)decoder

或者更好的方法是(正如理查德提到的)在 - (id)initWithCoder:(NSCoder *)decoder

"When instantiating a view controller from a storyboard, iOS initializes the new view controller by calling its initWithCoder: method instead." from documentation

“当从故事板实例化一个视图控制器时,iOS 通过调用它的 initWithCoder: 方法来初始化新的视图控制器。” 来自文档

But please note, if you're changing any data from one view controller to another you might want to consider holding this data in a separate part of code (so called modelin model-view-controllerapproach). This would be the safest way to 'exchange' data between viewControllers.

但请注意,如果您要将任何数据从一个视图控制器更改为另一个视图控制器,您可能需要考虑将这些数据保存在代码的单独部分(modelmodel-view-controller方法中称为)。这将是在 viewController 之间“交换”数据的最安全的方式。