I have my view controller class MyVC extending from UIViewController class. In the designated initializer I change the background color to GREEN as following
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.view setBackgroundColor:[UIColor greenColor]];
}
return self;
}
I also have the loadView method that creates a new UIView object and changes its color to RED
- (void)loadView
{
UIView* view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
[view setBackgroundColor:[UIColor redColor]];
[self setView:view];
[view release];
}
The designated initializer is called before loadView call. So I expect that my view color (which I set GREEN in designated initializer) should become RED (which I did in loadView).
I see my color GREEN and if I comment that GREEN color line in designated initializer, then I see the RED color. So why is it not overriding the view properties in loadView method if it is called after initializer?
The purpose of
-loadViewis to, uh, load the view. It’s called when you access the view controller’sviewproperty and the value of that property is nil. In this case, you’re accessingself.viewin your initializer, so that’s when-loadViewgets called. You set the view’s background after that happens, so the view ends up with a green background.