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
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";
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);
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
Thanks for any kind of help!
maybe I'm answer to late but, you can take a look at the Singleton Pattern.
Here is an ObjC implementation: http://getsetgames.com/2009/08/30/the-objective-c-singleton/
Check if
application:didFinishLaunchingWithOptions:
is being called by adding aNSLog(@"...")
. IftrackerTEST
is nil, it was possibly not correctly initialized.Change
@synthesize testGlobal = _testGlobal;
to@synthesize testGlobal;
and try again.Why you have @property tracker and var tracker?
Try to remove
MyClass *tracker;
property only should be enough.