Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

iphone - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...