Home / iPad

Understanding How the View is Initialized

If you look back to previous tutorial "How an App Runs", you can see that the template supplies the following code in DeepThoughtsAppDelegate.h:

@class DeepThoughtsViewController;
@interface DeepThoughtsAppDelegate : NSObject
	<UIApplicationDelegate> {
    UIWindow *window;
DeepThoughtsViewController *viewController;
}

This sets up the UIApplicationDelegate protocol with window. The template also declares an accessor method for window and tags it with an IBOutlet (so that Interface Builder can discover it) while also declaring an accessor method for viewController:

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) DeepThoughtsViewController
	  *viewController;

In the file DeepThoughtsAppDelegate.m, the @synthesize statements tell the compiler to create accessor methods for you - one for each @property declaration (window and viewController). After the delegate receives notification that the application has launched in the application:did FinishLaunchingWithOptions: method, the code uses the addSubview and makeKeyAndVisible methods to display the view:

@implementation DeepThoughtsAppDelegate
@synthesize window;
@synthesize viewController;

- (BOOL)application:(UIApplication *)application didF
	inishLaunchingWithOptions:(NSDictionary *)
	launchOptions {

    // Override point for customization after app launch
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

	return YES;
}

The view controller is initialized, and addSubView adds viewController. view to window in order to display the view. Calling makeKeyAndVisible on window makes the window visible as well as making it the main window and the first responder for events (touches).

In DeepThoughtsViewController.h, you find this:

@interface DeepThoughtsViewController : UIViewController {
}

This tells you that DeepThoughtsViewController is a subclass of UIViewController. The UIViewController class provides the fundamental view-management model for iPad apps. You use each instance of UIViewController to manage a full-screen view.

In DeepThoughtsViewController.m near the top, you find commentedout code you can use to set up custom view initialization:

/*
// The designated initializer. Override to perform setup
	that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil
	bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil
	bundle:nibBundleOrNil])) {
	// Custom initialization
    }
    return self;
}
*/

The DeepThoughtsViewController object is created by a nib file directly passed on to UIViewController to handle the initialization. (You can add custom initialization at the point where the // Custom initialization comment appears.)

[Previous] [Contents] [Next]