【发布时间】:2010-11-12 03:14:48
【问题描述】:
我正在使用 [[UIApplication sharedApplication] delegate] 在多个类之间共享一个变量。我在 AppDelegate 中设置了值。我可以从 myAppDelegate.m NSLog 它并查看值。然后,当我的一个选项卡加载时,我尝试 NSLog 值,它崩溃了:
myAppDelegate *app = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"Value:%@ ", app.delegateVar); // <--- Causes Crash
基本上好像它正在创建一个新的 app.delegateVar 实例?
delegateVar 在 myAppDelegate.h 中定义,然后在 myAppDelegate.m 中定义:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
[delegateVar release];
delegateVar = [NSString stringWithFormat:@"Test Value"];
NSLog(@"%@",delegateVar);
...
}
【问题讨论】:
-
delegateVar属性是什么以及它是如何定义的? -
您的应用委托类的名称是什么?正常的命名约定是让
AppDelegate.m包含一个名为AppDelegate的类,而不是myAppDelegate。 (并且类名按惯例大写,因此“myAppDelegate *app = ...”看起来很可疑。 -
命名不一致只是我发帖时编辑的错误。
-
delegateVar 是一个 NSMutableArray。我在 myAppDelegate.m 中定义它...请参阅上面的编辑。
-
“导致崩溃”如何?你到底遇到了什么错误?
标签: iphone variables delegates