DShah
DShah

Reputation: 9866

Is variable declared in AppDelegate accessible to all the other classes?

What exacly the AppDelegates method is given in Xcode?? I have so many classes in my app. Now i want is that i have AudioStreamer class and i have to use that class in most other classes... And I want to have only one instance of AudioStreamer class. So that it will be easy to handle one object. Is it feasible to declare AudioStreamer class in AppDelegate file and make instance in that file only... Can I access that variable in all the other class.???

Upvotes: 2

Views: 1109

Answers (3)

stack2012
stack2012

Reputation: 2186

You can also access the objects declared as properties in appDelegate thru out ur app like this.

myFirstAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
myStr=[appDelegate.mainArray objectAtIndex:1];

In the above example I have shown how to access the array which I have declared and retained in appDelegate class. In this way you can access any objects you want which are declared as properties,thru out ur app. Hope this helps.

Upvotes: 1

the.evangelist
the.evangelist

Reputation: 488

You could use very handy GCD (Grand Central Dispatch) functions to achieve Singleton behavior on these lines -

+ (AudioStreamer*) defaultStreamer {
    static AudioStreamer* defaultStreamer = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defaultStreamer = [[AudioStreamer alloc] init];
    });
    return defaultStreamer; 
} 

Upvotes: 2

Kay
Kay

Reputation: 13146

I would recommend a singleton so that only one instance is created and shared by all clients.

I suggest Matt Galaghers posting about singletons and his downloadable SynthesizeSingleton.h:

http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

Upvotes: 3

Related Questions