views:

65

answers:

2

Hi all

I am not clear about the need to create an app delegate class, instead of adding the delegate methods in the viewcontroller directly.

Is there any reason why someone want to have a separate delegate class? Style preference? Design constrains? (I am following the MVC model, as Xcode users and app makers do).

I am asking since i have 3 books that explain the process of writing apps in different ways; so I would like to know if is just personal preference of the authors, or if there is something different under the hood (all my C++ books were explaining the concepts in the same way; and now that i am using OBJ-C and cocoa touch, i feel totally disoriented when i see different implementations of the same app).

Another thing that make me think, is the need to create a separate class as viewcontroller, instead of using the template that is in Xcode directly (it creates a view controller automatically); since another book shows how they load the template but then they create a viewcontroller class from scratch....again personal preference of the author or there is something else going on?

Sorry if it sounds simple; but i am a beginner at OBJ-C :)

Thanks!

+2  A: 

A quick stroll through the application delegate protocol suggests that there are a lot of things for which an iOS app really should have an app delegate.

Just make sure not to roll too much stuff into that one object. From what I've read, it's all too common to make it a Big Ball of Mud class, rather than something strictly defined. Anything that doesn't directly relate to an app-delegate responsibility should go in some other controller. The app delegate is a good place to create and own those controllers, so it should be easy for the delegate to send any necessary messages (e.g., “hey, we just got a memory warning; clean up some stuff”) to them.

Peter Hosey
Thanks Peter.So your suggestion would be to keep the delegate class anyway, since it does not hurt to have all the delegates in one class aside the controller class? To me is better since from my past experience, i have the dogma of "1 class per action; and separate all that can be separate"; so for me is better to have more classes with specialized duty instead of a big ball of mud :)
newbiez
I didn't say put all the delegates in one class; I said you should have an app delegate in addition to your controllers. Those controllers should typically be the delegates of other things; the app delegate should be the app delegate alone.
Peter Hosey
+3  A: 

The app delegate provides a centralised, application-wide class that can be used to co-ordinate behaviour across your entire application. You can consistently access your App delegate using the [[UIApplication sharedApplication] delegate] method - this is incredibly powerful.

It's also responsible for handling iOS invoked behaviour (applicationDidBecomeActive, applicationWillTerminate, etc) so with that in mind it's a good place to wire some of your application together (perhaps you're loading from xib's and need to add views, for example) or to handle application lifecycle logic.

Note of caution; it is a little too easy to overuse the app delegate. Without considered design and development it can quickly become a unmaintainable mess. Personally I have a rule; I only use it for application lifecycle work and to coordinate between the component parts of my application, and it's rarely invoked directly from the UI layer.

dannywartnaby
Thanks Danny, you did a pretty clear picture; I was not aware that it was application wide; in this case there is no real advantage in adding the delegate in the viewcontroller (it would destroy the main reason for which the VC exist: serve one specific view and nothing else)
newbiez