ios - 添加了 iPad Storyboard,现在为 : "expected to have a root view controller..."

标签 ios xcode ipad storyboard appdelegate

我有一个仅限 iPhone 的应用程序,大部分是使用 Interface builder 构建的。

我为每个屏幕尺寸/设备都有一个 Storyboard。 在 AppDelegate 中,它选择正确的 Storyboard并加载它。

我重新设计了我的应用程序,现在添加了 iPad 兼容性。

我添加了两个新的 Storyboard,将它们的大小固定为“iPad Full Screen”和“iPad Pro Full Screen”。

我复制了现有的 iPhone Storyboard,并将第一个 Storyboard勾选为“是初始 View Controller ”。 我也关注了THIS POST并打开 Storyboard作为源代码并将目标从默认更新为 iPad。

当我在 iPad 上启动该应用程序时,出现黑屏并出现以下错误:

应用程序窗口在应用程序启动结束时应该有一个 Root View Controller

为什么会这样?

这是我在 AppDelegate 中为选择正确的 Storyboard所做的唯一更改:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UIViewController *initialViewController = nil;
CGSize result = [[UIScreen mainScreen] bounds].size;

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){

    if(result.height == 480){
        NSLog(@"iPhone 3.5 Inch");
        UIStoryboard *i4SB = [UIStoryboard storyboardWithName:@"iPhone35" bundle:nil];
        initialViewController = [i4SB instantiateInitialViewController];
    }
    if(result.height == 568){
        NSLog(@"iPhone 4 Inch");
        UIStoryboard *i5SB = [UIStoryboard storyboardWithName:@"iPhone4" bundle:nil];
        initialViewController = [i5SB instantiateInitialViewController];
    }
    if(result.height == 667){
        NSLog(@"iPhone 4.7 Inch");
        UIStoryboard *i47SB = [UIStoryboard storyboardWithName:@"iPhone47" bundle:nil];
        initialViewController = [i47SB instantiateInitialViewController];
    }
    if(result.height == 736){
        NSLog(@"iPhone 5.5 Inch");
        UIStoryboard *i55SB = [UIStoryboard storyboardWithName:@"iPhone55" bundle:nil];
        initialViewController = [i55SB instantiateInitialViewController];
    }

    //added iPad compatibility
    if(result.height == 1024){
        NSLog(@"iPad Mini(2,3,4) or iPad Air(1,2) or iPad(3,4) or iPad Pro(9.7inch)");
        UIStoryboard *iPadMiniSB = [UIStoryboard storyboardWithName:@"iPadFull" bundle:nil];
        initialViewController = [iPadMiniSB instantiateInitialViewController];
    }
    if(result.height == 1366){
        NSLog(@"iPad Pro(12.9 Inch)");
        UIStoryboard *iPadProSB = [UIStoryboard storyboardWithName:@"iPadPro" bundle:nil];
        initialViewController = [iPadProSB instantiateInitialViewController];
    }
}

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController  = initialViewController;
[self.window makeKeyAndVisible];
return YES;
}

更新/解决方案:

正如 Bharat Nakum 在下面提到的,您可以删除 UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone 条件。

当然你也可以给iPad加一个额外的条件(我就是这么做的)

现在看起来像这样:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
    //set iPhone device storyboards here
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
    //set iPad device storyboards here
}

这也被确认为答案here .

最佳答案

请删除此条件。

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

仅当设备为 iPhone 时为真。

因此,您的 iPad Storyboard未初始化。

希望这对您有所帮助!

关于ios - 添加了 iPad Storyboard,现在为 : "expected to have a root view controller...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39702264/

相关文章:

ios - 使用 Dropbox 选择器在 ipad 应用程序中导入 pdf

ios - UIImage 初始化选项

c++ - 你如何在 XCode + C++ 中使用 std::vector?

iOS 应用内购买噩梦

iphone - 开发者为什么要关心 ios 设备是否越狱

ios - Xcode 5 缩进问题

ios - 是否可以在类/结构实例方法中调用 GameScene 函数?

objective-c - 在 iOS 应用程序中创建自定义模板

iphone - 找不到 momd 文件 : Core Data problems

iPhone、iTouch、iPad 蓝牙连接到通用蓝牙设备