As Paul.s pointed out, you need to perform your custom code in viewDidLoad method.
From Apple documentation.
This method is called after the view controller has loaded its
associated views into memory. This method is called regardless of
whether the views were stored in a nib file or created
programmatically in the loadView method. This method is most commonly
used to perform additional initialization steps on views that are
loaded from nib files.
So, in your controller you could do this:
- (void)viewDidLoad
{
[super viewDidLoad];
// your other views here
// call addSubview method on self.view
}
Why do you do this? Because here you are sure that view has loaded in memory and outlets has been set correctly.
On the contrary, about loadView method
If you override this method in order to create your views manually,
you should do so and assign the root view of your hierarchy to the
view property. (The views you create should be unique instances and
should not be shared with any other view controller object.) Your
custom implementation of this method should not call super.
An example could be:
- (void)loadView
{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
// call addSubview method on self.view
}
I suggest you to read View Controller Programming Guide for iOS.
Hope it helps.
viewDidLoadwhich is called after the view has finished loading (from thenib). You only use theloadViewmethod if you are setting up your view completely in code.