I'd like to be able to simply call [UIStoryboard mainStoryboard]
to get either the iPad or iPhone storyboard at runtime.
Here's a UIStoryboard category that'll do just this:
#import <UIKit/UIKit.h>
@interface UIStoryboard (LDMain)
+ (instancetype)LDMainStoryboard;
@end
#import "UIStoryboard+LDMain.h"
UIStoryboard *_mainStoryboard = nil;
@implementation UIStoryboard (LDMain)
+ (instancetype)LDMainStoryboard {
if (!_mainStoryboard) {
NSBundle *bundle = [NSBundle mainBundle];
NSString *storyboardName = [bundle objectForInfoDictionaryKey:@"UIMainStoryboardFile"];
_mainStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
}
return _mainStoryboard;
}
@end
Here's a link to the gist
You can use [UIStoryboard storyboardWithName:storyboardName bundle:bundle]
if the storyboard has not already been loaded.
If it has, though, this will load a new copy.
You can also use viewController.storyboard
to get the existing one. If you have a mainWindow
as part of your application delegate (you probably do), you can get the rootViewController.storyboard
of that.
Something like:
UIApplication *application = [UIApplication sharedApplication];
MyAppDelegate *myAppDelegate = ((MyAppDelegate *)application).delegate;
return myAppDelegate.mainWindow.rootViewController.storyboard;
If not, this might work for you:
UIApplication *application = [UIApplication sharedApplication];
UIWindow *backWindow = application.windows[0];
return backWindow.rootViewController.storyboard
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