问题描述
我想使用我的AppDelegate来存储一个对象,任何其他类都可以访问。我已经声明这个AppDelegate像这样:
I would like to use my AppDelegate to store a object which will be accessible to any other classes. I've declared this AppDelegate like this :
@interface MyAppDelegate : UIResponder <UIApplicationDelegate>
{
MyClass *tracker;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ICViewController *viewController;
@property (retain, nonatomic) MyClass *tracker;
@end
我合成跟踪器和应用程序:didFinishLaunchingWithOptions:i set该对象中的一个NSString类似于:
I synthesize tracker and in application:didFinishLaunchingWithOptions: i set one NSString in that object like this :
self.tracker = [[MyClass alloc] init];
self.tracker.testGlobal = @"global funguje";
当我需要在其他类别的地方访问tracker时,使用这个代码:
When i need to access tracker somewhere else in some other class i use this code :
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
MyClass *trackerTEST = appDelegate.tracker;
NSLog(@"TEST : %@",trackerTEST.testGlobal);
问题是testGlobal是NULL。我做错了什么?这里还有MyClass的类文件:
The problem is that testGlobal is NULL. What am I doing wrong? Also here are class files for MyClass :
@interface MyClass : NSObject
{
NSString *testGlobal;
}
@property (retain, nonatomic) NSString *testGlobal;
@end
@implementation MyClass
@synthesize testGlobal = _testGlobal;
@end
感谢您的任何帮助!
推荐答案
检查应用程序:didFinishLaunchingWithOptions:
是否正在通过添加 NSLog @...)
。如果 trackerTEST
为nil,则可能无法正确初始化。
Check if application:didFinishLaunchingWithOptions:
is being called by adding a NSLog(@"...")
. If trackerTEST
is nil, it was possibly not correctly initialized.
这篇关于用作全局变量的AppDelegate的变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!