开发者

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

开发者 https://www.devze.com 2023-03-22 19:29 出处:网络
What exa开发者_Go百科cly 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.

What exa开发者_Go百科cly 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.???


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


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; 
} 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消