首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在运行时获取主情节提要

在运行时获取主情节提要
EN

Stack Overflow用户
提问于 2013-10-12 05:36:29
回答 2查看 7.9K关注 0票数 17

我希望能够简单地调用[UIStoryboard mainStoryboard]在运行时获取iPad或iPhone情节提要。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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

票数 21
EN

Stack Overflow用户

发布于 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
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19327646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档