
When you create a new project using a template — quite the normal state of affairs, as I explain in Chapter 3 — the basic application environment is included. That means when you launch your application, 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 as illustrated back in Figure 6-1.
But what does the UIApplicationMain function actually do? I’m glad you asked. When it goes through its paces, the process works more or less as follows, as illustrated in Figure 6-2.
1. An instance of UlApplication is created.
2. UlApplication looks in the info. plist file, trying to find the main nib file.
It makes its way down the Key column 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. nib.
3. UlApplication loads MainWindow. xib.
The file MainWindow. xib is what causes your application’s delegate, main 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.
UlApplication
<UIApplicationMain>
Figure 6-2:
The application is launched.
To take advantage of this once-in-a-lifetime opportunity, go back to your project window in Xcode, expand the Resources folder in the Groups & Files listing on the left and then double-click MainWindow. xib. (You do have a project, right? If not, check out Chapter 4.) When Interface Builder opens, take a look at the nib file’s main window — the one labeled MainWindow. xib, which should look like the MainWindow. xib you see in Figure 6-2. Double-click the ReturnMeToViewController object as well as the Window object, if they are not already open. You should end up with three windows open, as shown in Figure 6-3.
Figure 6-3 shows MainWindow. xib contains five files. The objects you see are as follows:
✓ A File’s Owner proxy object: The File’s Owner object is actually the UIApplication instance. 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.
✓ 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 to (usually) point to the object with which the user is currently interacting. If, for example, the user were to tap a text field to enter some data, the first responder would become the text field object.
✓ An instance of ReturnMeToAppDelegate set to be the application’s delegate.
✓ An instance of the ReturnMeToViewController.
✓ A window: The window has its background set to white and is set to be visible at launch. This is the window you’ll see when the application launches.
Okay, so all these disparate parts of the MainWindow. xib are loaded by UIApplication. What happens next is shown in Figure 6-4. The numbers in the figure correspond to the following steps:
1. Create ReturnMeToAppDelegate.
2. Create Window.
3. Create ReturnMeToViewController.
4. ReturnMeToViewController:LoadView loads the view from the ReturnMeToViewController. xib file.
Wait a sec — how does the ReturnMeToViewController know that it’s supposed to do that? If you double-click the
ReturnMeToViewController object in the MainWindow. xib window (refer back to Figure 6-3), you can see the ReturnMeToViewController window with its view. There, right in the middle of that view, it tells you that it will be "Loaded from ReturnMeToViewController. nib." (As I said in Chapter 4, a nib file type used to be the term of choice. Here is a vestige of that. Don’t worry; despite the nib business, it really will be the. xib file.) If you use the Inspector to look at the View Controller attributes (see Figure 6-5), you can see the NIB Name drop-down menu specifies said NIB name — the nib file for the view controller. In this case, you see ReturnMeToViewController specified. This makes the connection explicit between the MainWindow. xib and the ReturnMeToViewController. xib. All done without any fuss or bother, I might add, by Xcode when you created the project from the template.
Figure 6-3:
The application’s Main Window. xib as it appears in Interface Builder.
П П UqkilMndow. il lb
111 ■ I ш i л О
Figure 6-4:
Creating the App Delegate, window, view controller, and the view.
Figure 6-5:
Connecting the Main Window. xib and the ReturnMe ToView Controller xib.