Main Nib File
When you create a new project using a template - the basic application environment is already included. That means when you launch your app, an application object is created and connected to the window object, the run loop is established, and so on - despite the fact that you haven't done a lick of coding.
Most of this work is done by the UIApplicationMain function. To take advantage of this once-in-a-lifetime opportunity to see how all this works, go back to your project window in Xcode and select the Resources folder in the Groups & Files list on the left.
Here's a blow-by-blow description of what the UIApplicationMain function actually does:
- An instance of UIApplication is created.
- UIApplication looks in the info.plist file, trying to find the main nib file.
To see the info.plist file, select DeepThoughts-Info.plist in the Detail view of the Xcode window. The contents of the file appear in the Editor view below the Detail view.
UIApplication makes its way down the Key column of the info. plist file until it finds the Main Nib File Base Name entry. Eureka! It peeks over at the Value column and sees that the value for the Main Nib File Base Name entry is MainWindow. - UIApplication loads MainWindow.xib.
The nib file MainWindow.xib is what causes your application's delegate, window, and view controller instances to get created at runtime. Remember, this file is provided as part of the project template. You don't need to change or do anything here. This is just a chance to see what's going on behind the scenes.
To see the nib file (MainWindow.xib) in Interface Builder, select the Resources group - if it is not already selected - and double-click MainWindow.xib in the Xcode project window's Detail view.
When Interface Builder opens, take a look at the nib file's main window - the one labeled MainWindow.xib. Select File's Owner in the window, click the i Inspector button at the top of the window, and then click the i Identity tab of the Inspector window if it's not already selected (or choose Tools → Identity Inspector to show the Identity tab of the Inspector window).
The MainWindow.xib window shows five icons, but you can view them in a list by clicking the center View Mode button in the upper-left corner of the window. The interface objects are as follows:
- File's Owner (proxy object): The File's Owner - the object that's going
to use (or own) this file - is of the class UIApplication. This object
isn't created when the file is loaded, as are the window and views - it's
already created by the UIApplicationMain object before the nib file is loaded.
UIApplication objects have a delegate object that implements the UIApplicationDelegate protocol. Specifying the delegate object can be done from Interface Builder by setting the delegate outlet of a UIApplication object. To see that this has already been done for you in the template, click File's Owner and then click the Connections tab of the Inspector window (or choose Tools → Connections Inspector). The delegate outlet is set to "DeepThoughts App Delegate," - click the outlet connection in the Inspector window and DeepThoughts App Delegate is highlighted in the MainWindow.xib window. - First Responder (proxy object): This object is the first entry in an application's responder chain, which is constantly updated while the application is running - usually to point to the object that the user is currently interacting with. If, for example, the user were to tap a text field to enter some data, the first responder would become the text field object.
- Window: The window has its background set to white and its status bar set to gray and it is not set to be visible at launch. To see the window's attributes, click Window in the MainWindow.xib window and click the Attributes tab in the Inspector window.
- An instance of DeepThoughtsAppDelegate set to be the application's delegate: You can see the header and implementation of DeepThoughtsAppDelegate in Listings-1 and Listings-2 in the next section. This is where you can put code that restores the app after launch to its previous state (see "Responding to interruptions" in this tutorial for the meaning of this) or performs any other custom application initialization.
- An instance of DeepThoughtsViewController set to be the application's view controller: The view controller is where you put your code to control the views of your app.
The following section spells out what happens to these objects when UIApplication loads MainWindow.xib.