Alex Rivera | Logout

Dynamically load nib for iPhone/iPad within view controller

Asked 2010-06-16T10:05:06.027
21

I have converted an iPhone application using the wizard like thing in XCode into a universal app.

It builds fine but obviously looks a bit rubbish in some areas :)

I need to load nibs according to which device is being used. I dont wish to create my view controllers using initWithNib as I already have code to create the controllers with some data (initWithMyLovelyData) which doesnt do anything to do with nib loading.

I know to find out the device you use UI_USER_INTERFACE_IDIOM() so I tried overriding the initWithNibName within the actual view controllers themselves, assuming they get called internally somehow. But it's not working as I guess I am unsure of the syntax.

I have tried

if(ipad..) self = [super initWithNibName:@"MyIpadNib" bundle:nibBundleOrNil];

And that doesnt work :/

EDIT - I know I have massively edited this, made my question a bit more specific after doing some more research - apologies!

Edit
Report

1 Answer

2

You can have your cake and eat it too. Just add a method that wraps initWithNibName:bundle: and adds your myLovelyData parameter:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  myLovelyData:(id)data
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    {
        // Custom initialization using myLovelyData
        //
    }
    return self;
}
answered 2010-10-09T07:57:06.620

Your Answer