根据设备类型选择不同的故事板 [英] Selecting different storyboards based on device type
问题描述
我有一个通用应用程序,我在应用程序中手动加载我的主故事板:didFinishLaunchingWithOptions
。
I have a universal app in which I'm loading my main storyboard manually in application:didFinishLaunchingWithOptions
.
我有2个适用于iPhone和iPad的故事板,其中包含 ~iPhone
和 ~iPad
后缀。我正在使用以下方式加载我的故事板:
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];
这会在Interface Builder文件中输出未知类ViewController。
到控制台,显然它没有加载正确的故事板。但是,当我使用 [UIStoryboard storyboardWithName:@MainStoryboard~iPhonebundle:nil];
它工作正常,但当然只适用于iPhone。
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?
推荐答案
我不知道任何基于故事板的自动选择在他们的文件名后缀。您可以使用 userInterfaceIdiom
来选择iPad与iPhone:
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
或者 - 选择故事板中的视图控制器,转到属性instpector和tick isInitialViewController
Or - select the view controller in the storyboard, go to the attributes instpector and tick isInitialViewController
这篇关于根据设备类型选择不同的故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!