xcode 你如何在 Objective-C 中的视图控制器之间传递对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2770460/
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
How do you pass objects between View Controllers in Objective-C?
提问by buley
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.
两天来,我一直在艰难地浏览一些代码,试图弄清楚为什么我无法获取我在 .h 中声明并在 .m 中实现并在 viewDidLoad 函数中设置的全局 NSMutableArray 变量。
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."
我终于明白了:在Objective-C 中没有全局变量这样的东西,至少在我所了解的PHP 意义上没有。我从来没有真正阅读过 XCode 错误警告,但它确实存在,即使不是很简单的英语:“在类方法中访问了实例变量 'blah'。”
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.
我的问题:我现在应该做什么?我有两个视图控制器需要访问我通过 URL 从 JSON 文件生成的中央 NSMutableDictionary。它基本上是我所有 Table View 向下钻取的扩展菜单,我想要几个其他“全局”(非静态)变量。
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 时我都必须获取 JSON 还是有某种方法可以设置一次并通过#import 从各种类访问它?我是否必须将数据写入文件,或者人们通常是否有另一种方式来执行此操作?
回答by RedBlueThing
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?
如果您有两个访问共享 NSMutableDictionary 的视图控制器,您能否将指向公共字典的指针传递到它们各自的 init 消息中?
So in your AppDelegate:
所以在你的 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];
}
And in your respective UIViewControllerOne and UIViewControllerTwo implementations
并在您各自的 UIViewControllerOne 和 UIViewControllerTwo 实现中
- (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];
}
回答by David Gelhar
There are, in fact, a bunch of ways you can do this (without resorting to something extreme like writing to a file). You can create a "global" by using:
事实上,有很多方法可以做到这一点(无需采取诸如写入文件之类的极端方法)。您可以使用以下方法创建“全局”:
- Old-fashioned C global variables (externs)
- A Singletonclass
- instance variables on the Application delegate
- 老式 C 全局变量(externs)
- 一个单例类
- 应用程序委托上的实例变量
But all of these approaches make your view controller less modular (because they depend on reaching "outside" to find the global data), so the best approach is probably to make the dictionary a property of the view controller class that must be explicitly set by the caller (either within an initWithDictionary: method, or with a separate setter).
但是所有这些方法都使您的视图控制器不那么模块化(因为它们依赖于到达“外部”来查找全局数据),因此最好的方法可能是使字典成为视图控制器类的一个属性,该属性必须由调用者(在 initWithDictionary: 方法中,或使用单独的 setter)。
回答by Rudiger
There are global variables in Obj-C, you instantiate them in the App Delegate.
Obj-C 中有全局变量,你在 App Delegate 中实例化它们。
But onto your problem you probably want to pass the NSMutableDictonary when you instantiate the new view controller like [UIView alloc] initWithDictionary:(NSMutableDictionary *)dict; if you know what I mean.
但是对于您的问题,您可能希望在实例化新的视图控制器时传递 NSMutableDictonary,例如 [UIView alloc] initWithDictionary:(NSMutableDictionary *)dict; 如果你明白我的意思。
In your example are you having a single class calling another class? I would think there is some kind of controller that determines which view controller to display and that would be the place to access and pass the dictionary
在你的例子中,你有一个班级调用另一个班级吗?我认为有某种控制器可以确定要显示哪个视图控制器,这将是访问和传递字典的地方
回答by drawnonward
Just make a global variable. Global means outside any scope.
只需创建一个全局变量。全局意味着在任何范围之外。
NSMutableDictionary *gDictionary;
@implementation ...
@end