如何在Objective-C中的视图控制器之间传递对象? [英] How do you pass objects between View Controllers in Objective-C?
问题描述
我一直在跋涉一些代码两天,试图找出为什么我无法获取我在.h中声明并在.m中实现并在viewDidLoad函数中设置的全局NSMutableArray变量。
I've been trudging through some code for two days trying to figure out why I couldn't fetch a global NSMutableArray variable I declared in the .h and implemented in .m and set in a the viewDidLoad function.
我终于明白了:在Objective-C中没有全局变量这样的东西,至少在PHP意义上我不知道。我从未真正阅读过XCode错误警告,但即使不是很简单的英语也是如此:在类方法中访问实例变量'blah'。
It finally dawned on me: there's no such thing as a global variable in Objective-C, at least not in the PHP sense I've come to know. I didn't ever really read the XCode error warnings, but there it was, even if not quite plain English: "Instance variable 'blah' accessed in class method."
我的问题:我现在该怎么办?我有两个View Controller需要访问我通过URL从JSON文件生成的中央NSMutableDictionary。它基本上是我所有Table View钻取的扩展菜单,我想要其他几个全局(非静态)变量。
My question: What am I supposed to do now? I've got two View Controllers that need to access a central NSMutableDictionary I generate from a JSON file via URL. It's basically an extended menu for all my Table View drill downs, and I'd like to have couple other "global" (non-static) variables.
我有吗每次我想生成这个NSMutableDictionary时抓取JSON还是有一些方法可以设置它一次并通过#import从各个类访问它?我是否必须将数据写入文件,或者人们通常会采用其他方式吗?
Do I have to grab the JSON each time I want to generate this NSMutableDictionary or is there some way to set it once and access it from various classes via #import? Do I have to write data to a file, or is there another way people usually do this?
推荐答案
如果您有两个视图访问共享NSMutableDictionary的控制器,你可以将指向公共字典的指针传递到它们各自的初始化消息吗?
If you have two view controllers that access the shared NSMutableDictionary, can you pass a pointer to the common dictionary into their respective init messages?
所以在你的AppDelegate中:
So in your AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// the app delegate doesn't keep a reference this, it just passes it to the
// view controllers who retain the data (and it goes away when both have been released)
NSMutableDictionary * commonData = [[NSMutableDictionary new] autorelease];
// some method to parse the JSON and build the dictionary
[self populateDataFromJSON:commonData];
// each view controller retains the pointer to NSMutableDictionary (and releases it on dealloc)
self.m_viewControllerOne = [[[UIViewControllerOne alloc] initWithData:commonData] autorelease];
self.m_viewControllerTwo = [[[UIViewControllerTwo alloc] initWithData:commonData] autorelease];
}
在各自的UIViewControllerOne和UIViewControllerTwo实现中
And in your respective UIViewControllerOne and UIViewControllerTwo implementations
- (id)initWithData:(NSMutableDictionary*)data
{
// call the base class ini
if (!(self=[super init]))
return nil;
// set your retained property
self.sharedData = data;
}
// don't forget to release the property
- (void)dealloc {
[sharedData release];
[super dealloc];
}
这篇关于如何在Objective-C中的视图控制器之间传递对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!