开发者

Why we use app delegate in our application

I am new in iphone development and i needed a array which i use to access in different class so my senior told me that you should declare in App delegate and access it another class in which it require, so i wrote line for that

MyAppAppDelegate * appObject = 
                 (MyAppAppDelegate*)[[UIApplication sharedApplication]delegate];

I done my task successfully but i couldn't get it 100%. Someone tell that why we use this and in what exactly situation in which we've to 开发者_JS百科use this?


AppDelegate is loaded first when you run your application as it contains window. So, the variable and objects you want to access throughout your project is declared in AppDelegate. You just have to create an instance and you can access all the objects in AppDelegate.


ApplicationDelegate can be useful as a singleton class but you have to use it with discretion - and there are varying opinions on this - but if you have a few global type properties or methods you want to recall from various other classes, and I emphasize few, then ApplicationDelegate may be a nice place to add these.

And yes, it is bad design - but you can get away with it if you are prudent and as @Sedate Alien mentions, take a look at dependency injection.

The purpose of ApplicationDelegate, by the way, is mainly to handle events like loading your application, when you return to home screen, when you come back from home screen, etc.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜