Where does the application delegate come from?

Posted on October 10, 2008

When I first starting looking at how XCode and Interface Builder worked together, it wasn’t obvious how the application delegate was created. So, here’s my sequence of what happens.

  • The application loads its default nib file. That’s normally MainWindow.nib, but you’ll want to look at Info.plist in your project, under “Main nib base project:” screenshot

  • The “File’s Owner” for the default nib file is the UIApplication. For other nib files, you’ll specify the file owner in the call that loads the nib.

  • The nib file has an object for your application delegate (in this case, called “Tst2 App Delegate”). It’s a top-level object so it’s instantiated when the nib file is loaded. It’s connected to the ‘delegate’ field of the File’s Owner: Interface Builder

  • When the nib file is loaded, top-level objects are instantiated for you. In this example, “Tst2 App Delegate” and “Tst2 View Controller” will be created automatically.

  • Tst2 App Delegate is connected to the delegate outlet of the File’s Owner - in this case, the UIApplication object for your app.

  • Tst2 View Controller is connected to the Tst2 App Delegate’s viewController outlet: Interface Builder

= Documentation

You’ll want to read or at least skim these documents, found here on the developer website or in the XCode doc.

Resource Programming Guide - much more detail on how resource files (including nibs) are used.

Baby’s First iPhone Application - See Dick and Jane write an iPhone app. Surprisingly useful quick tour through creating an app. (And the real title is Your First iPhone Application, in case you were wondering.)