loading different storyboard causing error - ios

I am trying to load different storyboard but am getting this error Application windows are expected to have a root view controller at the end of application launch. Can anyone help me out
- (UIStoryboard *)grabStoryboard {
UIStoryboard *storyboard;
// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;
if (height == 480) {
storyboard = [UIStoryboard storyboardWithName:#"iPhone_4" bundle:nil];
// NSLog(#"Device has a 3.5inch Display.");
}
if(height == 568)
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone_5" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
if(height == 667)
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone_6" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
if(height == 736)
{
storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
return storyboard;
}

You are getting this error because compiler see the case when your function can return uninitialised storyboard variable. It might happened if non of the if statements is true. So it's better to rewrite this function so it always return initialised storyboard variable. My suggestion is to change the way how to determine device model. Take a look at this post for hint.

i think one or more of your storyboards doesn't have an entry point make sure to verify them all . you can check the official doc here entry point for storyboard

Related

Why won't my storyboard work on the iphone?

In the app delegate I have this written to determine which storyboard will be opened depending on the screen size,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [self grabStoryboard];
// show the storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
- (UIStoryboard *)grabStoryboard {
UIStoryboard *storyboard;
// detect the height of our screen
int width = [UIScreen mainScreen].bounds.size.width;
if (width == 480) {
storyboard = [UIStoryboard storyboardWithName:#"iphone4" bundle:nil];
// NSLog(#"Device has a 3.5inch Display.");
} else {
storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
return storyboard;
}
The app opens landscape on the device.
This will properly choose between a 3.5 inch device and a 4 inch device in the simulators, but won't properly display in an actual 3.5 inch device. In Xcode 6 it opens the correct storyboard for the 4s or the 5, 6 ,6+ depending on the width of the screen but when tested on an actual Iphone 4s (with ios 7.1.1) the storyboard configured for 4 inch devices is displayed.

"Application windows are expected..." error when loading storyboard for Ipad

I'm loading different storyboards for Iphone and Ipad and the Iphone's storyboards load good but when I start the Ipad Simulator I got the "Application windows are expected..." error.
this is the code in AppDelegate:
#implementation AppDelegate
- (UIStoryboard *)grabStoryboard {
UIStoryboard *storyboard;
// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;
if (height == 480) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Iphone3" bundle:nil];
// NSLog(#"Device has a 3.5inch Display.");
}
if (height >= 1024 && height <= 2048 ) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Ipad" bundle:nil];
NSLog(#"Device has a 4inch Display.");
}
else {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Iphone4Up" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
return storyboard;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [self grabStoryboard];
// show the storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
In the console I see the NSLog message but the storyboard doesn't load and the "Application windows..." error appears.
I'm using some Scroll Views and some elements but i thinkg those are not the problem because there is an option for them "Adjust Scroll View Insets".
I uploaded some screen shots to show more about my project. Thanks for your time.
I figured it out. When you copy all stuff from the Iphone storyboard to the Ipad one the Initial View Controller checkbox is unchecked. Just check it and all will be good (:

Using multiple Storyboards in Xcode

I have created 5 extra Storyboards and I have deleted the main storyboard. This is because I would like to have 5 separate storyboards for the iPhone and iPad to match 5 different screen sizes. So far I have one for the iPhone 3.5 inch, one for the iPhone 4 inch, one for the iPhone 4.7 inch, one for the iPhone 5.5 inch and one for the iPad. I have put the code bellow in to link it all up and make it work. However, it doesn't work when you try and build the project. There is no errors but lets say I go into the iPhone 3.5 inch storyboard and I add a UIViewController and a Button or label, then when you build the project it goes to your launch screen and then it doesn't do anything from there. I have put the starting arrow in at the UIViewController but I cannot get anything to come up apart from the launch screen. I have tried this on all the storyboards and their simulators. I am wondering if I have missed something out in the code but I am not sure.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyboard = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPad" bundle:nil];//iPad
} else {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 480){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone4S" bundle:nil];//iPhone 3.5inch
} else
if (screenSize.height == 568){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone5/5C/5S" bundle:nil];//iPhone 4inch
}
else
{ if (screenSize.height == 667){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone6" bundle:nil];//iPhone 4.7inch
} else
if (screenSize.height == 736){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone6Plus" bundle:nil];//iPhone 5.5inch
} else
//default storyboard
storyboard = [UIStoryboard storyboardWithName:#"Main.Storyboard" bundle:nil];
}
}
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
Find your info.plist file in your project and remove the line called "Main storyboard file base name".
Also, you forgot to create the window (credits to rdelmar).
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
I strongly advise you to look into Auto Layout and Size Classes. Using these two, you can support all screen sizes within one single Storyboard. It's a bit hard to get in the beginning, but it'll definitely be worth it in the long run.
There's a video about this from the most recent WWDC called Building Adaptive Apps with UIKit.

Adding a second storyboard causes "could not find storyboard" error

I started by building 2 versions of my app, on for 3.5 inch screens and one for 4 inch with only the storyboard being different. I brought the 3.5 storyboard into the 4 inch project and used the following code in my appDelegate.m to have the program run the appropriate storyboard.
UIViewController *vc;
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568.0f)
{
NSLog(#"IPHONE IS WORKING");
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"Main.storyboard" bundle:nil];
vc = [storybord instantiateInitialViewController];
}
else
{
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"MainFour.storyboard" bundle:nil];
vc = [storybord instantiateInitialViewController];
}
}
else
{
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"iPad" bundle:nil];
vc = [storybord instantiateInitialViewController];
}
[_window setRootViewController:vc];
[_window makeKeyAndVisible];
When I use this, I get the error:
"Could not find a storyboard named 'Main.storyboard' in bundle
NSBundle"
The error happens even if I try and use the storyboard that was not imported, the one that runs if the above code is missing. So I am assuming that the error lies in the name of the storyboard. In the project navigator, the storyboard is named "Main.storyboard" but calling that name does not find it. What am I doing wrong? Thanks.
Drop the .storyboard extension from the name you specify.
From the reference:
storyboardWithName:bundle:
Creates and returns a storyboard object for the specified storyboard resource file.
+ (UIStoryboard *)storyboardWithName:(NSString *)name
bundle:(NSBundle *)storyboardBundleOrNil
Parameters
name The name of the storyboard resource file without the filename extension. This method raises an exception if this parameter is nil.

How can I load different storyboards depending on the device?

I am trying to:
detect device (iPhone 3.5", iPhone 4", iPad, ect)
load a different storyboard depending on what device and what size the application is running on.
I have watched some tutorials but I am still not getting it, can someone please type/show what code needs to go in the app delegate to achieve these goals.
Thanks!
Try something like this in your app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIStoryboard *storyboard = nil;
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568.0f)
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone5s" bundle:nil];
}
else
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone4" bundle:nil];
}
}
else
{
storyboard = [UIStoryboard storyboardWithName:#"iPad" bundle:nil];
}
[window setRootViewController:[storyboard instantiateInitialViewController]];
[window makeKeyAndVisible];
}

Resources