views:

75

answers:

2

I don't understand the concept of delegation is used in XCode. When a new project is created, an app delegate and a view controller are created, but what does the app delegate do? How does main.m know to call the delegate?

+4  A: 

main.m doesn't call the delegate. The application object does that at runtime to let you customize the behaviour of your application without needing to subclass or do any other funny business. Delegation a pretty common feature of the Apple frameworks; reading their documentation or the Wikipedia page on delegation might be good choices to learn more.

Carl Norum
Specifically, it takes a look at the Info.plist file to determine what NIB file to load at startup, which creates your app delegate from there
Ed Marty
A: 

main.m is a file.

The main function in main.m calls NSApplicationMain (or the UIKit equivalent if you're asking about Cocoa Touch).

NSApplicationMain runs the shared NSApplication object.

The NSApplication object talks to its delegate.*

The delegate of the shared NSApplication object is called, by Cocoa programmers, the application delegate.

As Carl Norum suggested, you should read the section on delegates in the Cocoa Fundamentals Guide. It will explain the delegate concept more generally; the application delegate is but one example.

*When exactly it does this depends partly on when the delegate is instantiated and assigned as the application's delegate. Putting it in MainMenu.nib and hooking it up to the application object's delegate outlet is one way. There are others, but that's the easiest.

Peter Hosey