簡體   English   中英

如果有一個nib文件,會調用loadView嗎?

[英]Will loadView be called if there is a nib file?

如果我覆蓋loadView方法,則會調用loadView是否存在nib文件。 如果我不覆蓋loadView並且有一個nib文件,它會被調用嗎?

是的, loadView負責根據視圖控制器的類名自動從已知的bundle加載nib文件。 如果覆蓋loadView並且不調用[super loadView] ,則不會加載任何nib。 UIViewController類在調用view屬性時調用loadView並且為nil。

另請注意,覆蓋loadView和調用super很可能不是您想要的。 loadView用於設置self.view屬性,就是這樣。 其他一切都應該在viewDidLoad等中發生。

是。 無論bundle是否具有Nib,都將始終調用Load視圖。 loadView將為UIViewController加載視圖,可以來自任何源。

因此,它以兩種方式尋找觀點

1.檢查屬性nibFile ,如果你調用了initWithNibName:bundle ,可以設置或者如果你有故事板 - storyboard也存儲在nib文件中創建的所有視圖

2.如果您沒有從任何一個源設置nib,那么您計划自己創建視圖。 在這種情況下,您可以將此視圖設置為稍后查看控制器的視圖屬性。

是:

@implementation iPhoneHomeViewController

- (void)loadView {
    DEBUGLOG(@"view = %@, superview = %@", [self valueForKey:@"_view"], [[self valueForKey:@"_view"] superview]);
    [super loadView];
}

安慰:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM