我希望能够简单地调用[UIStoryboard mainStoryboard]
在运行时获取iPad或iPhone情节提要。
发布于 2013-10-12 05:36:29
下面是一个UIStoryboard类别,它可以做到这一点:
UIStoryboard+LDMain.h
#import <UIKit/UIKit.h>
@interface UIStoryboard (LDMain)
+ (instancetype)LDMainStoryboard;
@end
UIStoryboard+LDMain.m
#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
这是一个link to the gist
发布于 2013-10-12 06:38:00
如果脚本尚未加载,则可以使用[UIStoryboard storyboardWithName:storyboardName bundle:bundle]
。
如果有,这将加载一个新的副本。
您也可以使用viewController.storyboard
来获取现有的。如果你有一个mainWindow
作为你的应用程序委托的一部分(你可能有),你可以得到它的rootViewController.storyboard
。
类似于:
UIApplication *application = [UIApplication sharedApplication];
MyAppDelegate *myAppDelegate = ((MyAppDelegate *)application).delegate;
return myAppDelegate.mainWindow.rootViewController.storyboard;
如果不是,这可能对你有用:
UIApplication *application = [UIApplication sharedApplication];
UIWindow *backWindow = application.windows[0];
return backWindow.rootViewController.storyboard
https://stackoverflow.com/questions/19327646
复制相似问题