如何将两个xib放在iphone中的同一视图控制器中

时间:2014-01-04 05:42:20

标签: ios iphone objective-c

我正在尝试为同一个视图控制器制作两个xib。我有一些代码,但我不知道将根据代码视图名称是什么。这个代码我必须放在视图中加载吗?

这是我的代码:

if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
    NSLog(@"Ipad ");
}
else
{
    NSLog(@"Iphone %f ",[[UIScreen mainScreen] bounds].size.height);
    if ([[UIScreen mainScreen] bounds].size.height == 568) {
        viewName=[[NSString alloc]initWithString:@"ViewControllerIphone5"];
         //this is iphone 5 xib
    } else {
        viewName=[[NSString alloc]initWithString:@"ViewController"];
         // this is iphone 4 xib
    }
}

请帮助

提前致谢

4 个答案:

答案 0 :(得分:1)

DetailView *detailView;

    if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)//for iPad
    {
       detailView = [[DetailView alloc]initWithNibName:@"DetailView_iPad" bundle:nil];
    }
    else //this is for iphone
    {
      CGSize result = [[UIScreen mainScreen] bounds].size;
    if (result.height==480)
    {
         detailView = [[DetailView alloc]initWithNibName:@"DetailView_iPhone" bundle:nil];
         //this is iphone 4 xib
    }
    if (result.height==568)
    {
        detailView = [[DetailView alloc]initWithNibName:@"DetailView_iPhone5" bundle:nil];
         // this is iphone 5 xib
    }

    }
 [self.navigationController pushViewController:detailView animated:YES];

答案 1 :(得分:0)

我这样做了

#define IS_IPHONE_5 (CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size, CGSizeMake(640, 1136)))


UIViewController *homeViewController;
    if (IS_IPHONE_5) {
        homeViewController = [[LGHomeViewController alloc] initWithNibName:@"LGHomeViewController" bundle:nil];
    } else {
        homeViewController            = [[LGHomeViewController alloc] initWithNibName:@"LGHomeViewController_IPhone4" bundle:nil];
    }

答案 2 :(得分:0)

试试这段代码..

    #define IDIOM    UI_USER_INTERFACE_IDIOM()
    #define IPAD     UIUserInterfaceIdiomPad
    #define IPHONE   UIUserInterfaceIdiomPhone

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            // The device is an iPad running iPhone 3.2 or later.

           NSLog(@"Ipad ");

        }
        else
        {
            // The device is an iPhone or iPod touch.

             YourViewController *yourVC;

            CGRect screenBounds = [[UIScreen mainScreen] bounds];

            if (screenBounds.size.height == 568)
            {
            yourVC = [[YourViewController alloc] initWithNibName:@"YourViewController_iPhone5" bundle:nil];
            } 
           else 
           {
            yourVC= [[YourViewController alloc] initWithNibName:@"YourViewController_iPhone4" bundle:nil];

           }
     }

答案 3 :(得分:0)

您需要在init上加载正确的NIB,而不是viewDidLoad。这应该做到,但正如其他人指出autolayout是一般的方式。

- (id)init

    if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
        return [self initWithNibName:@"ViewControllerIpad" bundle:nil];
    }
    else if ([[UIScreen mainScreen] bounds].size.height == 568)
    {
        return [self initWithNibName:@"ViewControllerIphone5" bundle:nil];
    } 
    else 
    {
        return [self initWithNibName:@"ViewController" bundle:nil];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}