Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting different storyboards based on device type

I have a universal app in which I'm loading my main storyboard manually in application:didFinishLaunchingWithOptions.

I have 2 storyboards for iPhone and iPad which have the ~iPhone and ~iPad suffixes. I'm loading my storyboard using:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
self.initialViewController = [storyboard instantiateInitialViewController];

This prints Unknown class ViewController in Interface Builder file. to the console, so apparently it's not loading the correct storyboard. However, when I use [UIStoryboard storyboardWithName:@"MainStoryboard~iPhone" bundle:nil]; it works fine, but of course will work only for iPhone.

What am I missing? How can I use the name suffixes to automatically select the correct storyboard?

like image 605
Hesham Avatar asked Feb 03 '13 11:02

Hesham


2 Answers

I am not aware of any automatic selection of storyboards based on their filename suffix. You can use userInterfaceIdiom to select for iPad vs iPhone:

if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad) {
    UIStoryboard *storyboard = 
    [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
} else {
    [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
}

But if you are doing this to start up with a specifc view controller, all you need to do is drag the 'start' arrow to your preferred view controller in the storyboard

Or - select the view controller in the storyboard, go to the attributes instpector and tick isInitialViewController

like image 104
foundry Avatar answered Oct 06 '22 01:10

foundry


This is another thing you can set directly within your info.plist file. No need for any programming efforts. Look for the property named 'Main storyboard file base name' that will have 'Main' in it by default.

You can add another property named 'Main storyboard file base name (iPad)' that will then get used for iPad.

This is what the raw output in the plist looks like:

<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIMainStoryboardFile~ipad</key>
<string>iPad</string>

Afaik it is also possible to simply add a second storyboard named Main~iPad.storyboard (if the UIMainStoryboardFile key is set as Main). And that will get picked up for iPads. Haven't tested this in a while though.

like image 44
Ville Rinne Avatar answered Oct 05 '22 23:10

Ville Rinne