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

左心房为你撑大大i 提交于 2020-01-19 20:29:39

问题


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:

- (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?


回答1:


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

- (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.




回答2:


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.




回答3:


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

NSNibAwaking Protocol Reference (requires ADC login)



来源:https://stackoverflow.com/questions/492106/does-interface-builder-use-the-init-method-to-initialize-view-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!