Add a UINavigationController

After we have learned how to use the Xcode documentation in part 3 of the iPhone tutorial, let’s add a navigation controller that provides the typical navigation bar that will host the navigation controls like the back button:

Navigation Controller

But before that, some more basics. We have seen the call to UIApplicationMain() in main.m:

int retVal = UIApplicationMain(argc, argv, nil, nil);

From the documentation one can learn that there is an application delegate object which takes control as soon as the application is started. UIApplicationMain loads this object from the application's main nib file by default. Let’s have a look at how this works.

NIB files are created by the graphical user interface editor of Xcode. They have the file extension .xib:

MainWindow.xib

NIB/XIB files contain "pickled" objects. Our .xib file contains two objects, an App Delegate and a Window:

Inspect MainWindow.xib

When the app starts, the .xib file is loaded by UIApplicationMain and all the objects come to life. Let’s have a look at the App Delegate. If you inspect this object, you can see that this object is created from one of our project classes:

How the app delegate is loaded

If you have a look the ShoppingListAppDelegate.m implementation, you’ll see that this class makes the window visible:

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window makeKeyAndVisible];
    return YES;
}

This is the Window object which is loaded from our .xib file. We will have a deeper look in the connection between classes and objects from .xib files in a later part of the tutorial after we went through some basics of the Objective C language.

So now we know: Our application delegate is loaded from our .xib file and makes the Window from the .xib file visible. Let’s play around with the window a bit. For example, change the background color:

Change the window background color in the NIB

If you run the app now, you will see the Window with the changed background color.

You can add UI objects by dragging them from the Object Library:

Adding a control in interface builder

Let’s see if we can add a UINavigationController the same way. If you try it, you will learn that you cannot drag the navigation controller on the Window. This is because a navigation controller is not a view itself, so it cannot be added to a Window. A Controller contains and manages a View, but it is not a view itself. But we can add it besides the Window as separate object:

Adding a UINavigationController in interface builder

The important point here is: where is the connection between the Window that will be on the screen and the Navigation Controller? Currently there is none, we just added another object which will not be used. We have to establish a connection by telling our Window object that it should use our navigation controller. This is done by setting the property rootViewController on the Window object:

Set the NavigationController as rootViewController for the Window

If you run the app now, you’ll see that the view controller is used by the Window:

Window with NavigationController

About the author

Ralf Ebert Ralf Ebert is an independent software developer and trainer for Mac OS X and iOS. He makes the Page Layers, TakeOff and Straight ahead apps and conducts iOS trainings and Git trainings in Germany.