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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With