如何在多个视图/XIB 之间共享变量 [英] How to share a variable between multiple Views/XIBs
问题描述
我已经阅读了几个类似的主题,但似乎无法解决这个问题 - 抱歉.我有一个包含多个视图的项目,包括一个 ViewController、几个子"视图和一个 NavigationController.我想要一个可从任何视图访问的数组,以便我可以填充 TableView 或 TextField 等.我希望该数组可以从任何可以访问它的视图中修改.
I've read several similar threads, but can't seem to figure this out - sorry. I've got a project with multiple Views, including a ViewController, several "sub" views, and a NavigationController. I'd like to have an array that is accessible from any of the Views, so that I can populate a TableView or a TextField, etc. I'd like the array to be modifiable from any View that can access it.
现在我在 xxAppDelegate.h 文件中有这个:
Right now I have this in the xxAppDelegate.h file:
NSMutableArray *listOfHeadings;
@property (strong,nonatomic) NSMutableArray *listOfHeadings;
并在 xxAppDelegate.m 文件中填充数组:
And in the xxAppDelegate.m file I populate the array:
listOfHeadings = [[NSMutableArray alloc] init];
[listOfHeadings addObject:@"Heading 0"];
[listOfHeadings addObject:@"Heading 1"];
但我似乎无法从其他 .m 文件访问数组.我该如何正确执行此操作?
But I can't seem to access the array from other .m files. How do I do this correctly?
推荐答案
使用当前的设计,您可以使用以下方法获取 AppDelegate 指针:
With the current design you have, you can get ahold of the AppDelegate pointer using:
AppDelegate *dPtr = (AppDelegate *)[[UIApplication sharedApplication] delegate];
然后您可以使用该属性访问您的数组:
Then you can access your array using the property:
[dPtr.your_array objectAtIndex:i];
但我强烈建议使用单例设计模式来管理您的全局对象.
But I would highly recommend using a Singleton design pattern to manage your global objects.
这篇关于如何在多个视图/XIB 之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!