質問

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.

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.

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

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top