iphone - 根据设备类型选择不同的 Storyboard

标签 iphone ios uistoryboard

我有一个通用应用程序,我在 application:didFinishLaunchingWithOptions 中手动加载我的主 Storyboard。

我有 2 个用于 iPhone 和 iPad 的 Storyboard,它们具有 ~iPhone~iPad 后缀。我正在使用以下方式加载 Storyboard:

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

这会将 Unknown class ViewController in Interface Builder file. 打印到控制台,所以显然它没有加载正确的 Storyboard。但是,当我使用 [UIStoryboard storyboardWithName:@"MainStoryboard~iPhone"bundle:nil]; 它工作正常,但当然只适用于 iPhone。

我错过了什么?如何使用名称后缀自动选择正确的 Storyboard?

最佳答案

我不知道有任何基于文件名后缀的 Storyboard自动选择。您可以使用 userInterfaceIdiom 来选择 iPad 还是 iPhone:

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

但是如果你这样做是为了启动一个特定的 View Controller ,你需要做的就是将“开始”箭头拖到 Storyboard中你喜欢的 View Controller 上

或者 - 在 Storyboard中选择 View Controller ,转到属性 instpector 并勾选 isInitialViewController

关于iphone - 根据设备类型选择不同的 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14672023/

相关文章:

html - 如何删除 Iphone 上的电话号码链接?

ios - 从远程服务器调用返回并评估错误对象时崩溃

ios - 将 UITabBarController 与 UINavigationController 结合使用 - Swift 3

ios - 标题下方 NavigationBar 上的 UiSegmentedControl

iphone - 超正方 OCR : How to find the read-error-magnitude of each returned character?

ios - 创建全局变量的最佳实践

iphone - UIToolbar 中的持久 UIBarButtonItem?

ios - 将 CURL 转换为 URLRequest

ios - 使用 TitleView 在 IOS 的导航栏上添加 2 行文本

objective-c - NSFetchedResultsController 保留对 Storyboard中已释放委托(delegate) Controller 的引用,导致崩溃