Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initWithNibName is not getting called

I need to put it some custom logic into my iPhone app so that depending on what iOS version you are running, choose a different XIB file (i.e. iPhone or iPad will show different XIB files).

I had built the whole iPhone app from day one and its all good, using a tabbarcontroller and the standard navigation controllers in each tab.

So I implemented the :

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle

method, only to find that it does not get called. (I have a breakpoint and log statements in it and nothing gets hit).

Does anyone know why this might be? OR how can i achieve this functionality?

I have read somewhere that initWithNibName is only called when you call it, i.e. when you programatically construct your view hierarchy, is this true??

like image 656
Mark Avatar asked Sep 07 '10 23:09

Mark


2 Answers

If you're not doing something like this before adding it to your navigation controller:

SomeViewController *someViewController = [[SomeViewController alloc] initWithNibName:...];

Then chances are good you're overriding the wrong method.

If your view controller lives in a nib file, say your navigation controller's nib file, it will be unarchived from the nib file as a new view controller object, instead of being created with its own nib file, so the above method won't be called.

You'll have to override -awakeFromNib instead.

like image 187
BoltClock Avatar answered Oct 23 '22 16:10

BoltClock


If your view controllers are loaded from a .xib file then initWithCoder: will be called instead. The initWithNibName:bundle: method is only used for programatically creating view controllers.

like image 6
Stefan Arentz Avatar answered Oct 23 '22 16:10

Stefan Arentz