开发者

Will loadView be called if there is a nib file?

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 ?


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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜