Interface Builder是否使用-init方法初始化視圖控制器?

[英]Does Interface Builder use the -init method to initialize view controllers?


I have setup tab bar controller using interface builder, and each tab bar item is linked to a view controller (4 tabs, 4 view controllers). I want to know if Interface Builder uses an -init method to initialize the view controller because apparently this method does not get called:

我使用界面構建器設置了標簽欄控制器,每個標簽欄項目都鏈接到一個視圖控制器(4個標簽,4個視圖控制器)。我想知道Interface Builder是否使用-init方法來初始化視圖控制器,因為顯然不會調用此方法:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;

... and I want to do some initializations. I can't add that to -viewDidLoad since it is recalled in case of memory warning. Any idea?

......我想做一些初始化。我無法將其添加到-viewDidLoad,因為在內存警告的情況下會調用它。任何的想法?

3 个解决方案

#1


Objects loaded from a *.(nib|xib) are inited with:

從*。(nib | xib)加載的對象包含:

- (id)initWithCoder:(NSCoder *)inCoder;

So you could override that or if doing your setup after -initWithCoder: is called is not a problem you could use:

所以你可以覆蓋它,或者如果在-initWithCoder之后進行設置:被調用不是你可以使用的問題:

- (void)awakeFromNib;

from the NSNibAwaking protocol.

來自NSNibAwaking協議。

#2


I was also going to mention initWithCoder vs awakeFromNib.

我還要提到initWithCoder和awakeFromNib。

In general, I override initWithCoder when allocating memory for the object or setting values. When you need to do some setup after the IBOutlets are connected, then override awakeFromNib. Until then, IBOutlet instance variables to other views and controls are not connected.

通常,我在為對象分配內存或設置值時覆蓋initWithCoder。在連接IBOutlet后需要進行一些設置時,請覆蓋awakeFromNib。在此之前,IBOutlet實例變量與其他視圖和控件沒有連接。

#3


Sounds like you want to implement -(void) awakeFromNib.

聽起來你想要實現 - (void)awakeFromNib。

NSNibAwaking Protocol Reference (requires ADC login)

NSNibAwaking協議參考(需要ADC登錄)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/01/29/7206ef694e8bb393b60ed6b26d810458.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com