If I override the loadView method, loadView will be called whether there is a nib file. If I don't override loadView and there is a nib file, will it be called ?
3 Answers
Yes, loadView
is responsible for loading nib files automatically from known bundles based on the class name of the view controller. If you override loadView
and don't call [super loadView]
, no nibs will be loaded. The UIViewController class will call loadView
when its view
property is called and is nil.
Also note that overriding loadView
and calling super is most likely not what you want. loadView
is for setting the self.view
property, that's it. Everything else should happen in viewDidLoad
etc.

- 54,010
- 13
- 102
- 111
-
What if there is no view in the nib file? Will loadview be called if there is view in nib file? – Terry Sep 24 '11 at 08:21
-
`loadView` is always called. If the nib doesn't have a `view` property, or references IBOutlets that aren't present in your view controller, an exception is thrown. Haven't verified, but I'm pretty sure the exception comes when `loadView` invokes UINib after finding a nib matching it's class name or loading the nib specified in `initWithNibName:`. – August Lilleaas Sep 25 '11 at 10:07
Yes. Load view will be always gets called irrespective of bundle has a Nib or not. loadView will has the job of loading view for UIViewController which can come from any source.
So, it look for views in two ways
1. Check the property nibFile which can be set if you have called initWithNibName:bundle: or if you have storyboard - storyboard stores all the views created inside a nib file too
2. If you don't set nib from either of the source then you are planning to create views yourself. In this case you can set this view to view controller's view property later.

- 19
- 1
- 2
Yes:
@implementation iPhoneHomeViewController
- (void)loadView {
DEBUGLOG(@"view = %@, superview = %@", [self valueForKey:@"_view"], [[self valueForKey:@"_view"] superview]);
[super loadView];
}
Console:
GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul 1 10:50:06 UTC 2011)
Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU
General Public License, and you are welcome to change it and/or distribute copies of it
under certain conditions. Type "show copying" to see the conditions. There is absolutely
no warranty for GDB. Type "show warranty" for details. This GDB was configured as
"x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 1671.
[Line: 40] -[iPhoneHomeViewController loadView]: view = (null), superview = (null)

- 51,908
- 16
- 134
- 170