在通用应用程序中获取主要故事板

时间:2022-10-11 09:26:08

Trying my hand at a universal project, I'd like to load a vc by identifier from storyboard. Is there a way to avoid an explicit check of the idiom when accessing the storyboard.

试着在一个通用项目上,我想从故事板中加载一个vc标识符。有没有办法避免在访问故事板时明确检查成语。

This ugly code works....

这丑陋的代码有效....

UIStoryboard *storyboard;

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

UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

But yuck. The string literal main storyboard is hard enough to look at (not sure why the project cannot save a main storyboard setting so that apps can say [UIStoryboard mainStoryboard];) but the explicit idiom check is a bridge too far.

但是你好。字符串文字主要故事板很难看(不知道为什么项目不能保存主故事板设置,以便应用程序可以说[UIStoryboard mainStoryboard];)但显式的成语检查是一个过头的桥梁。

Is there any hidden intelligence (comparable to "@2x" image suffixes supporting retina displays) that could clean this code?

是否有任何隐藏的智能(可与支持视网膜显示的“@ 2x”图像后缀相比)清除此代码?

1 个解决方案

#1


16  

Assuming that you're executing the posted code from inside another view controller, you can always get the current storyboard via [self storyboard], where self is any instance of a UIViewController.

假设您正在从另一个视图控制器内部执行发布的代码,您可以始终通过[self storyboard]获取当前的故事板,其中self是UIViewController的任何实例。

#1


16  

Assuming that you're executing the posted code from inside another view controller, you can always get the current storyboard via [self storyboard], where self is any instance of a UIViewController.

假设您正在从另一个视图控制器内部执行发布的代码,您可以始终通过[self storyboard]获取当前的故事板,其中self是UIViewController的任何实例。