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?


3 个解决方案


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:


- (void)awakeFromNib;

from the NSNibAwaking protocol.



I was also going to mention initWithCoder vs 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.



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

听起来你想要实现 - (void)awakeFromNib。

NSNibAwaking Protocol Reference (requires ADC login)




粤ICP备14056181号  © 2014-2020