Вопрос

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