Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get main storyboard in universal app

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?

like image 381
danh Avatar asked Apr 11 '13 15:04

danh


1 Answers

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.

like image 125
Desmond Avatar answered Oct 26 '22 06:10

Desmond