Reputation: 4254
I know that we can easily use the objects declare in our appDelegate class by doing this
myAppDelegate *alpha= (myAppDelegate *)[[UIApplication sharedApplication]delegate];
hence from above we can use the alpha object to fetch the value of other objects in myAppDelegate class.
but suppose if I have a Class A and i have declared a NSString *hello in its .h and synthesize it in .m file.
Now in Class B I create an object of class A i.e
A *classA = [[A alloc]init];
A.hello = [NSString stringWithFormat:@"Kawa banga"];
[classA release];
Now in Class C I create an object of Class A again
A *classA = [[A alloc] init];
NSLog(@"%@",classA.hello);
It gives me null.
How can I get the value of my hello object in different class.
Upvotes: 0
Views: 381
Reputation: 1704
When you declare hello
as a property of class A
, it’s an instance variable. That means it’s separate for each instance of A
you create (with alloc
& init
).
It looks like you might want to share just 1 instance of A
. An easy way of doing this is to add it as a property of your myAppDelegate
class. (By the way, class names in Cocoa are usually begin with upper-case letter to distinguish them for variable names)
Once you’ve done that, you’ll be able to access it with:
myAppDelegate *alpha = (myAppDelegate *)[[UIApplication sharedApplication]delegate];
myAppDelegate.classA.hello = @"hello, world";
Upvotes: 1
Reputation: 17478
Either you have to use Singleton pattern or pass the object to the required class.
I think you need to go through on the Singleton Pattern
Upvotes: 1