UIViewController Subclassing: UINavigationController and UIView Lazy-Loading PDF Print E-mail
Written by William Grim   
Tuesday, 24 February 2009 05:31

UPDATE: As of SDK 3.0, this problem has disappeared. However, I have left the following article for those still wishing to work in the older SDKs.

I'm a bit of a newb to iPhone development and Cocoa-based development in general. However, I've been getting my hands dirty on a project I'm working on with some friends, and I found myself wanting to subclass a UIViewController in order to push it onto a UINavigationController. Needless to say, I ran into a few snags along the way and thought I would share some tips that may save you a few hours of frustration.

Basically, when you subclass UIViewController and push that onto a UINavigationController, UINavigationController doesn't do the proper calls to ensure your custom UIViewController instantiates its view property. Interestingly enough, UIViewController subclasses do not do this by default either. Because of this, if you don't instantiate it yourself in initWithNibName:bundle: or just before you push your view controller on the navigation controller's view controller stack, you will get a null pointer exception. The following code demonstrates what to do in your view controller to ensure it instantiates your UIView:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		[self view];
    }
    return self;
}

- (void)viewDidLoad {
	self.title = @"OtherView";
}

Simply by referencing the view property of your custom view controller, the base UIViewController will instantiate the UIView and call the appropriate view.*Load events. alternatively, you could call loadView yourself, but it will not call the view.*Load events.

Last Updated on Tuesday, 23 June 2009 18:36
 
viagra real | where to buy viagra | viagra information | cialis generic canada | order viagra online | cheap viagra online | generic viagra online | cialis no prescription | order viagra online | purchase viagra online