1.9. Presenting and Managing Views with UIViewController

Problem

You want to switch among different views in your application.

Solution

Use the UIViewController class.

Discussion

Apple’s strategy for iOS development was to use the model-view-controller (MVC) division of labor. Views are what get displayed to users, while the model is the data that the app manages, or the engine of the app. The controller is the bridge between the model and the view. The controller, or in this case, the view controller, manages the relationship between the view and the model. Why doesn’t the view do that instead? Well, the answer is quite simple: the view’s code would get messy, and that design choice would tightly couple our views with the model, which is not a good practice.

View controllers can be loaded from .xib files (for use with Interface Builder), or simply be created programmatically. We will first have a look at creating a view controller without a .xib file.

Xcode helps us create view controllers. Now that you have created an application using the Empty Application template in Xcode, follow these steps to create a new view controller for your app:

  1. In Xcode, select the File menu and then choose New New File...

  2. In the New File dialog, make sure iOS is the selected category on the left and that Cocoa Touch is the chosen subcategory. Once you’ve done that, select the New Objective-C class item on the righthand side and press Next, as shown in Figure 1-24.

New Objective-C subclass

Figure 1-24. New Objective-C subclass

  1. On the next screen, make sure that the “Subclass of” the text field says UIViewController. Also make sure that neither the “Targeted for iPad” nor the “With XIB for user interface” checkboxes is selected, as shown in Figure 1-25. Press Next.

A custom view controller with no .xib file

Figure 1-25. A custom view controller with no .xib file

  1. On the next screen (Save As), give your view controller’s file the name of “ViewController” and then press the Create button, as shown in Figure 1-26.

  1. Now find your application delegate’s .m file, which is usually called AppDelegate.m. In this file, declare a property of type ViewController:

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()
@property (nonatomic, strong) ViewController *viewController;
@end

@implementation AppDelegate

...
Saving a view controller without a .xib file

Figure 1-26. Saving a view controller without a .xib file

  1. Now find the application:didFinishLaunchingWithOptions: method of the app delegate and instantiate the view controller and set it as the root view controller of your window:

- (BOOL)            application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    self.viewController = [[ViewController alloc] initWithNibName:nil
                                                           bundle:nil];

    self.window = [[UIWindow alloc]
                   initWithFrame:[[UIScreen mainScreen] bounds]];

    /* Make our view controller the root view controller */
    self.window.rootViewController = self.viewController;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Go ahead and run the app on the simulator. You will now see a plain white view on the screen. Congratulations! You just created a view controller, and now you have access to the view controller and its view object.

While creating the view controller (Figure 1-25), if you had selected the “With XIB for user interface” checkbox, Xcode would have also generated a .xib file for you. In that case, you can load your view controller from that .xib file by passing the .xib file’s name (without the extension) to the initWithNibName parameter of the initWithNibName:bundle: method of the view controller, like so:

- (BOOL)            application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    self.viewController = [[ViewController alloc]
                           initWithNibName:@"ViewController"
                           bundle:nil];

    self.window = [[UIWindow alloc]
                   initWithFrame:[[UIScreen mainScreen] bounds]];

    /* Make our view controller the root view controller */
    self.window.rootViewController = self.viewController;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

If you did create a .xib file while creating your view controller, you can now select that file in Xcode and design your user interface with Interface Builder.

Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.