Connect Two XIBs To One ViewController

断了今生、忘了曾经 提交于 2019-12-18 07:18:00

问题


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

回答1:


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




回答2:


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:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/9661919/connect-two-xibs-to-one-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!