Objective c 根据应用程序运行的设备,从不同的XIB初始化rootViewController

Objective c 根据应用程序运行的设备,从不同的XIB初始化rootViewController,objective-c,interface-builder,ios-4.2,Objective C,Interface Builder,Ios 4.2,嘿,伙计们, 我有以下问题: 我有一个IPad应用程序,现在我希望它也能在IPhone上运行。(我知道应该是相反的,但我不能改变它)。 因此,现在我想通过使用以下命令来决定需要哪个视图: if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // The device is an iPad running iOS 3.2 or later. } else { // The device is an iPhone

嘿,伙计们, 我有以下问题: 我有一个IPad应用程序,现在我希望它也能在IPhone上运行。(我知道应该是相反的,但我不能改变它)。 因此,现在我想通过使用以下命令来决定需要哪个视图:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
     // The device is an iPad running iOS 3.2 or later.
}
else {
     // The device is an iPhone or iPod touch.
}
这适用于除RootViewController之外的所有视图。
无论我在哪里为RootViewController设置xib,它总是显示我为IPad应用程序定义的xib

这是我在应用程序代理中的代码:

viewController = [VoycerUniversalAppViewController sharedInstance]; 


UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window addSubview:myNavigationController.view];   
//[self.window addSubview:self.vcSplashScreen.view];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];

return YES;  

这是我的[VoycerUniversalAppViewController sharedInstance]中的代码:

+ (VoycerUniversalAppViewController*) sharedInstance 
{
    @synchronized(self) 
    {
        if(sharedInstance == nil) 
        {
            // only allocate here - assignment is done in the alloc method
            if (![AppState sharedInstance].loggedIn) 
            {
                [AppState sharedInstance ].loggedIn = FALSE;            
            }
        }
    }   
    return sharedInstance;
}

+ (id) allocWithZone:(NSZone*)zone {
    @synchronized(self) {
        if(sharedInstance == nil) {
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
            {
                // The device is an iPad running iOS 3.2 or later.
                NSLog(@"The device is an iPad running iOS 3.2 or later.");
                sharedInstance = [[super allocWithZone:zone] initWithNibName:@"VoycerUniversalAppViewController" bundle:nil];
            }
            else {
                // The device is an iPhone or iPod touch.
                NSLog(@"The device is an iPhone or iPod touch.");
                sharedInstance = [[super allocWithZone:zone] initWithNibName:@"VoycerUniversalAppIPhoneViewController" bundle:nil];
            }   
            // allocate and assign instance variable on first allocation                    
            return sharedInstance;
        }
    }
    return nil;
}

我几乎找遍了所有的地方,我想如果我错过了一些场景或什么的话,但我找不到任何地方。
你们有没有人知道为什么总是加载VoycerRunVersaLappViewController而不是另一个?
两个XIB链接到同一类。
如果这个解决方案很简单,很明显,请不要责怪我,我对xcode、IB和objective-c都是新手(我现在用objective-c编写代码1个半月了)

提前支付Thx。

Maverick1st.

对于主NIB,在
info.plist
文件中有设置:
NSMainNibFile
NSMainNibFile~ipad


在NIB文件中,您可以创建应用程序代理并设置导航控制器。

当我启动项目时,我制作了一个MainWindow.xib,其中包含我的代理和我的ViewController,因为我的一位同事告诉我这样做。如果我在MainWindow.xib中更改相应视图控制器的xib文件,它对视图没有影响。它仍然显示错误的一个。因为我在IPad和IPhone上只有一个MainWindow.xib,所以我将info.plist中的两个nsmainnib文件都设置为MainWindow。或者我应该为IPhone制作一个全新的MainWindow.xib吗?但是thx的提示,即使他们没有工作,现在。这对我来说是有用的信息。:)是的,创建两个笔尖并将其添加到info.plist。这应该行得通,可惜不行。我创建了一个IPhone_MainWindow.xib并将其设置为nsmainibile~ipad,但尽管它进入了其他程序{//该设备是IPhone或iPod touch.NSLog(@“该设备是IPhone或iPod touch.”);sharedInstance=[[super allocWithZone:zone]initWithNibName:@“VoycerUniversalAppIPhoneViewController”bundle:nil];}它不会用正确的笔尖显示我的视图。如何将iphone视图的xib连接到mainWindow.xib?我只能设置ViewController类和委托,而不能设置xib.ok,现在我让我的IPhone_MainWindow.xib为我的RootViewController加载正确的xib文件。我只是忘记在IPhone_MainWindow.xib中为ViewController设置正确的NibFile。Thx的帮助如果你的意思是我应该点击钩子,那么我已经做了:)。