views:

94

answers:

2

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

thanks a lot...

A: 

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.

BoltClock
Ok, I see, thanks, so how could I select the XIB from that method?
Mark
What do you mean by selecting the xib?
BoltClock
well I want different XIB files depending on if im running on the iPad or the iPhone, how could I achieve this?
Mark
Ah, well I'm afraid I'm not familiar with universal apps so I can't help you on that, sorry :/
BoltClock
thats ok, I have found out how to do it...1. Update your target to be a single universal application2. Make note of the new XIB file XXX-iPad.xib that xcode creates for you3. Create new XIB files that correspond to your iPad application (you can create new XIB files that point to your existing UIViewController's, just add a single "View XIB", change its class to your existing view controller)4. Inside that XIB file, change the normal XIB files that load to point to the new XIB files you made in step 35. Connect up your IBOutlet's like you would normally in your new XIB
Mark
A: 

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.

St3fan
ok, so how could I select the XIB from that method?
Mark