Jtaylorapps
Jtaylorapps

Reputation: 5770

Connect Two XIBs To One ViewController

I am, in all basics, wanting to have a view for iPhones, and one for iPads. However, I want to use the same view controllers for both of them, since they are the same thing, just optimized for each device.

I know it's possible to do this, because it is a universal app and the default views for a universal project include one main view for iPhones, and one main view for iPads. However, they're implemented automatically and so I don't know how to replicate this.

So, the jist of this question is: How do you have two xibs connected to one viewcontroller?

Thanks,

Upvotes: 1

Views: 4230

Answers (4)

Symanski
Symanski

Reputation: 337

Dante has answered the problem I had with this. "Above that...one more important step is to go in each xib file..click on File Owner (Left column).. and in the i*dentity inspector* (icons on the top right side) .. make sure it is of class YOurViewController"

But I'll add that set all your buttons up in one Nib. Then place them but don't connect them in the second the way you would with the assistant view. Instead right click on File Owner in the left column and you'll see a whole table of your buttons and outlets ready to be connected to the objects in the new Nib.

Hope this helps.

Upvotes: 0

sudip
sudip

Reputation: 2850

From code :

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

videosController = [[SoftLensAdvanceSearch alloc] initWithNibName:@"VideosController_ipad" bundle:nil];
}
else{//iphone
videosController = [[SoftLensAdvanceSearch alloc] initWithNibName:@"VideosController" bundle:nil];
}

From IB

Create 2 different version of "xib" file and specify the "file name" (1st group in properties window in xcode 4.3+), ie., for iphone add the iphone file name and for ipad add the ipad filename.

Upvotes: 0

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

First when you are loading the view you might have to check what device is it. If iPhone xib1 otherwise xib2. Regaring how to load the xib itself. its wasy. When you want to change views in a UIViewController just just use this code:

NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"xib2" owner:self options:nil];
UIView *aView    = [nibObjs objectAtIndex:0];
self.view = aView;

Upvotes: 0

Shubhank
Shubhank

Reputation: 21805

in code.. wherever you lot the new view ..do like this

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
            self.MainView = [[[YOurViewController alloc] initWithNibName:@"YOurViewController_iPad" bundle:nil]autorelease];

}
else
{
    self.MainView = [[[YOurViewController alloc] initWithNibName:@"YOurViewController_iPhone" bundle:nil]autorelease];
}

You will have to replace the nib name and class name with yours..

Above that...one more important step is to go in each xib file..click on File Owner.. and in the i*dentity inspector* (icons on the top right side) .. make sure it is of class YOurViewController

Upvotes: 9

Related Questions