引用AppDelegate实例变量 [英] Referencing AppDelegate instance variables
问题描述
我有一个基于导航应用程序模板的项目。
在AppDelegate中有方法 -applicationDidFinishLoading:
和 -applicationWillTerminate:
。在这些方法中,我加载和保存应用程序数据,并将其存储在一个实例变量(实际上是一个对象图)。
I have a project that is based on the Navigation Based Application template.
In the AppDelegate are the methods -applicationDidFinishLoading:
and -applicationWillTerminate:
. In those methods, I am loading and saving the application data, and storing it in an instance variable (it is actually an object-graph).
它加载MainWindow.xib,它有一个NavigationConroller,它又有一个RootViewController。 RootViewController nibName
属性指向RootView(我的实际控制器类)。
When the application loads, it loads MainWindow.xib, which has a NavigationConroller, which in turn has a RootViewController. The RootViewController nibName
property points to RootView (my actual controller class).
在我的类中,到我在 -applicationDidFinishLoading:
方法中创建的对象,以便我可以获得对它的引用。
In my class, I wish to refer to the object that I created in the -applicationDidFinishLoading:
method, so that I can get a reference to it.
任何人都可以告诉我该怎么做?我知道如何引用我以编程方式创建的对象,但我似乎无法找出来回我的方式,因为中间的步骤是从NIB文件内完成。
Can anyone tell me how to do that? I know how to reference between objects that I have created programmatically, but I can't seem to figure out to thread my way back, given that the middle step was done from within the NIB file.
推荐答案
对于我需要在应用程序中的任何地方访问它的变量(通常是模型数据结构),在AppDelegate类中声明它们。当你需要引用它:
For variables (usually the model data structure) which I need to access it anywhere in the app, declare them in your AppDelegate class. When you need to reference it:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
//and then access the variable by appDelegate.variable
这篇关于引用AppDelegate实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!