views:

55

answers:

2

Ok, I'm totally new to obj-c + cocoa, so this is probably obvious, but here goes:

I've been moving from command line apps to cocoa apps in learning how to work with objective-c in Xcode. One thing I don't really understand is the role of the AppDelegate and how it connects to main.m

It seems like you could put your entire program in the appdelegate and it would run fine, and you don't even need main.m, but not the other way around, if you're making a cocoa app you have to at least have the appdelegate.

I've done a lot of php web development and command-line tools, so I guess what I'm looking for is the file that the program will execute first and is intended to "control" the rest of them.

Can anyone help me understand what's going on in a Cocoa program, how AppDelegate and main.m are (or are not) related, and what the flow of the program is supposed to be?

+4  A: 

main.m contains the main() function, which is the entry point for the program, it's run first. Then it calls UIApplicationMain(), which does the OS-specific application setup stuff, and loads the main Interface Builder .xib file which contains your app delegate instance.

That is, without main.m your app delegate wouldn't even get loaded.

jtbandes
An important point is that `main.m` is basically boilerplate code that just sets everything up to run `UIApplicationMain()`. There is rarely a need to add or change anything in `main()`. So the app delegate is where "your code" starts.
Kristopher Johnson
Ok, this answer and the comment make sense, main() is just getting the program started and the first thing it does is get the UI going which means the app delegate is running.
Andrew
@Andrew Yeah, that's correct. It should also be noted that `UIApplicationMain()` can create your app delegate directly if you want, rather than loading it from a nib file (you pass the class name as one of the arguments). If my answer worked for you, please mark it as correct by clicking the checkmark under the vote count. Thanks :)
jtbandes
+3  A: 

A key feature of many object-oriented systems (such as Cocoa) is "inversion of control", which basically means that the framework is running everything, and any code you write is under its control.

So, unlike PHP, you don't really write the code that executes at startup. What you do is define methods for the app delegate, controllers, views, and other objects, and let the framework invoke those methods as it needs to do so. So you will never really see the overall "flow of control" throughout the program; you will only see it as control flows into your pieces of the program.

This can be confusing at first, as you try to figure out how to trick the framework into calling your code at the times and in the order you expect, but in the long run it actually makes things easier, as you can trust the framework to take care of a lot of things for you.

In a Cocoa app, a lot of the logic of the app will actually be in view controllers, rather than in the app delegate. The app delegate generally handles startup and shutdown responsibilities, but other objects do most of the work between startup and shutdown. So don't try to squeeze everything into the app delegate.

Kristopher Johnson
+1. All great info, though doesn't directly address the question. See jtbandes' answer for the main vs app delegate part.
quixoto
It doesn't address the part of the question about main.m vs. app delegate, but I think it does address the "what the flow of the program is supposed to be" part, which is what I think the questioner is really trying to figure out.
Kristopher Johnson
This helps a lot!The part I've been having a hard time grasping is what exactly needs to happen in the code for the framework to be able to use a particular method when I want it to.What is the exact connection between a controller file (controller.m and controller.h - for instance) and the UI. Am I supposed to hook the input to the AppDelegate and then have the AppDelegate relay data to and from the controller, or make the connection from the UI to the controller directly?
Andrew
All I can say is "it depends". If you have some global application-wide data that needs to be accessible to multiple controllers, then the app delegate may be the place to put it. But data that is used only by a controller and any subcontrollers it creates itself should reside in that controller. This article may help: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
Kristopher Johnson