I’ve started working on an application for the iPhone that I’ve been wanting to work on for ages. Actually to be factually correct I’ve restarted working on that application. I say that because my bouts of working on this thing are so far between that I always forget some of the basics I learned the last time around. This series of postings will be an attempt to chronicle those things to keep a record for myself and for anyone else who might be starting to do iPhone development. It is also a good exercise in being better able to retain what I’m learning by writing it down here.
Ever wonder how the app knows that MainWindow.xib is to be loaded at launch? I did I always wondered how exactly the system knew to load MainWindow.xib at launch. Was it some convention? Is it stored somewhere? Thing is its documented out there in several places. My problem is I haven’t spent enough time, until today, looking at the documentation.
Before I go any further I assume you’re using the project templates provided by Xcode to start your project. I also assume, since you’re reading this, that you are starting with an Xcode template All that being said here’s how it works.
How It Works
When you created your project via one of the templates Xcode created some files for you (the location of which depends on the version of Xcode you are using. Xcode 4 structures things a bit different than Xcode 3). The most important ones to this discussion are:
- <project name>-Info.plist
Lets talk about them one at a time.
This is an InterfaceBuilder nib file. It, at a minimum, contains the main (and most likely only) Window for your application. If you inspect the nib and take a look at the properties for File’s Owner you’ll see that UIApplication is the owner of this particular nib. This makes sense since this is the main window for your whole application.
This file is the entry point of your application and provides the initial bootstrapping of your application when it is launched by a user. Inside of main.m is a function call UIApplicationMain. That function is where MainWindow.xib is ultimately loaded.
This file contains a bunch of information used to configure your application at launch. Here is where we finally see how the application knows which nib to load when the application is launched. Inside of this plist file there is a section of code:
There are several places where this value can be set. The most obvious choice is editing the plist file itself and modifying that key. In Xcode 4 it can also be set in the project editor in two different places.
Targets Summary Page
With the project navigator opened in the left-hand pane click on the project itself which brings up the project editor. In the project editor select the application name listed under Targets. On the Summary page the entry for “Main Interface” will have “MainWindow” set. If you have more than one nib in the project the others will appear here as well. Choosing something else here will set the value in the plist for you.
Targets Info Page
The value can also be changed on the Info page of the Targets editor. The values for the entire plist show up under “Custom iOS Target Properties”. Editing the value in “Main nib file base name” will also change the value in the plist.
It is important to point out the .h and .m files that have been created by Xcode for your application delegate. Xcode automatically wires up this application delegate for you inside of MainWindow.xib. Once again clicking on File’s Owner look at the Connections inspector. Under “Outlets” there is a delegate wired there that corresponds to the application delegate created by the template.
One thing about using templates to create application skeletons is that sometimes it’s hard to understand just how things work. It allows a developer new to iOS programming to get up and running quickly but at the expense of really understanding just how things are wired together under the hood. I hope this posting helps a bit in understanding just what is happening with all of the pieces that Xcode is creating by default.