in my AppDelegate I have imported the header of a class I have created and propertied and syntesized an instance of it (in AppDelegate). Now I'm trying to access a method and a variable inside this instance from two other views. I'm relatively new to objective-c, but so far I've learned that if I do this:
AppDelegate *appdelegate = [AppDelegate new];
I will just get a fresh instance from the class inside AppDelegate, so if I set a variable from one view, I can't access it from the other. I've read that if I would do it this way:
AppDelegate *ap = (AppDelegate *)[[UIApplication sharedApplication] delegate];
It would allow me to access the existing instance. But it doesn't work.
Is the way I'm trying to do this totally wrong? Thanks a lot for your help!
UPDATE:
When I do this inside AppDelegate:
myClass.string = @"test";
NSLog(@"appDelegate: %@", myClass.string);
I get this:
appDelegate: (null)
UPDATE2:
I wrote @class AppDelegate; underneath the @import lines in the viewController, but still I can't access myClass. A main problem, which may be the cause why this isn't working from the views, is that I can't even access myClass from AppDelegate.
In AppDelegate.h I wrote:
@property (strong, nonatomic) testClass *myClass;
In AppDelegate.m:
#import "testClass.h"
@synthesize myClass;
This should be right, right?
myClass.h
@property (strong, nonatomic) NSString *string;
myClass.m
@synthesize string;
When I then try to access myClass from appDelegate, I write:
self.myClass.string = @"test";
NSLog(@"appDelegate: %@", self.myClass.string);
The result is:
appDelegate: (null)