Skip to content

A 1.9. Presenting and Managing Views with UIViewController

rayastar edited this page Feb 15, 2015 · 2 revisions

Problem

You want to switch among different views in your application.

Solution

Use the UIViewController class.

Apple’s strategy for iOS development was to use the model-view-controller (MVC) di‐ vision 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 con‐ troller 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. 3. On the next screen, make sure that the “Subclass of ” the text field says UIView Controller. Also make sure that neither the “Targeted for iPad” nor the “With XIB for user interface” checkboxes is selected 4. On the next screen (Save As), give your view controller’s file the name of “View‐ Controller” and then press the Create button. 5. Now find your application delegate’s .m file, which is usually called App Delegate.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
  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:@"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;

}

While creating the view controller, 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 initWithNib Name:bundle: method of the view controller, like so:

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

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.


Clone this wiki locally