0

大家好,我有以下问题:我有一个 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-App 定义的那个。

这是我在 App-Delegate 中的代码:

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;
}


如果我错过了一些设置或什么,我几乎到处寻找,但我找不到任何东西。
你们有谁知道,为什么不断加载 VoycerUnversalAppViewController 而不是另一个?
两个 Xib 都链接到同一个类。
如果解决方案非常简单且显而易见,请不要怪我,我对 xcode、IB 和 Objective-c 真的很陌生(我现在在 Objective-c 中编码 1 1/2 个月)。

提前谢谢。
小牛1st。

4

1 回答 1

2

对于主 NIB,文件中有设置info.plistNSMainNibFileNSMainNibFile~ipad.

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

于 2011-01-17T17:39:18.077 回答