Connect Two XIBs To One ViewController

2019-01-27 04:01发布

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,

  • Jake

4条回答
可以哭但决不认输i
2楼-- · 2019-01-27 04:11

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;
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-27 04:20

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.

查看更多
看我几分像从前
4楼-- · 2019-01-27 04:21

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.

查看更多
等我变得足够好
5楼-- · 2019-01-27 04:31

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

查看更多
登录 后发表回答