iPhone SDK: what is the difference between loadView and viewDidLoad? iPhone SDK: what is the difference between loadView and viewDidLoad? objective-c objective-c

iPhone SDK: what is the difference between loadView and viewDidLoad?


I can guess what might be the problem here, because I've done it:

I've found that often when I add init code to loadView, I end up with an infinite stack trace

Don't read self.view in -loadView. Only set it, don't get it.

The self.view property accessor calls -loadView if the view isn't currently loaded. There's your infinite recursion.

The usual way to build the view programmatically in -loadView, as demonstrated in Apple's pre-Interface-Builder examples, is more like this:

UIView *view = [[UIView alloc] init...];...[view addSubview:whatever];[view addSubview:whatever2];...self.view = view;[view release];

And I don't blame you for not using IB. I've stuck with this method for all of Instapaper and find myself much more comfortable with it than dealing with IB's complexities, interface quirks, and unexpected behind-the-scenes behavior.


loadView is the method in UIViewController that will actually load up the view and assign it to the view property. This is also the location that a subclass of UIViewController would override if you wanted to programatically set up the view property.

viewDidLoad is the method that is called once the view has been loaded. This is called after loadView is called. It is a place where you can override and insert code that does further initial setup of the view once it has been loaded.


viewDidLoad()

is to be used when you load your view from a NIB and want to perform any customization after launch

LoadView()

is to be used when you want to create your view programmatically (without the use of Interface Builder)