添加了 iPad 个故事板,现在:"expected to have a root view controller..."
Added iPad Storyboards, Now: "expected to have a root view controller..."
我有一个只有 iPhone 的应用程序,大部分是用 Interface builder
.
构建的
每个 screensize/device 我都有一个故事板。
在 AppDelegate
中,它 select 是正确的故事板并加载它。
我重新设计了我的应用程序,现在添加了 iPad 兼容性。
我添加了两个新的故事板,将它们的大小固定为 "iPad Full Screen" 和 "iPad Pro Full Screen"。
我复制了现有的 iPhone 个故事板,并将第一个故事板勾选为 "Is Initial View Controller"。
我还关注 THIS POST 并打开故事板作为源代码并将目标从默认更新为 iPad。
当我在 iPad 上启动应用程序时,出现黑屏并出现以下错误:
Application windows are expected to have a root view controller at the end of application launch
.
为什么会这样?
这是我在 AppDelegate
中唯一更改为 select 正确的 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 故事板没有初始化。
希望对您有所帮助!
我有一个只有 iPhone 的应用程序,大部分是用 Interface builder
.
每个 screensize/device 我都有一个故事板。
在 AppDelegate
中,它 select 是正确的故事板并加载它。
我重新设计了我的应用程序,现在添加了 iPad 兼容性。
我添加了两个新的故事板,将它们的大小固定为 "iPad Full Screen" 和 "iPad Pro Full Screen"。
我复制了现有的 iPhone 个故事板,并将第一个故事板勾选为 "Is Initial View Controller"。 我还关注 THIS POST 并打开故事板作为源代码并将目标从默认更新为 iPad。
当我在 iPad 上启动应用程序时,出现黑屏并出现以下错误:
Application windows are expected to have a root view controller at the end of application launch
.
为什么会这样?
这是我在 AppDelegate
中唯一更改为 select 正确的 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 故事板没有初始化。
希望对您有所帮助!