【问题标题】:How to know the current storyboard name?如何知道当前的故事板名称?
【发布时间】:2013-07-17 10:04:01
【问题描述】:

我想知道当前加载的故事板是什么,我使用了下面的代码,但它仍然让我得到主故事板而不是当前的。

//This get me the Main storyboard 
[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"];

【问题讨论】:

    标签: iphone ios storyboard uistoryboard uistoryboardsegue


    【解决方案1】:

    基于上面 Durican 的回答:

    在视图控制器中:

    UIStoryboard * storyboard = self.storyboard;
    NSString * storyboardName = [storyboard valueForKey:@"name"];
    

    (名称将不包括“.storyboard”文件扩展名。)

    【讨论】:

    • 完美解决方案。谢谢。
    • 问题。你是怎么想到这个的?该文档没有说明名称属性。我也看不到查询所有键的方法。你有没有从 IB 标签上看到它?
    • 只是一个完整的猜测,我测试了它,结果证明我是对的。 :)
    • 这是私有 API,所以要小心。
    【解决方案2】:

    如果您正在使用实例化的 UIViewController 类或通过情节提要进行切换,最简单的方法是:

    UIStoryboard *storyBoard = self.storyboard;
    

    如果你在 UIViewController 中的 UIView 类中

    UIResponder *responder = self;
    while (![responder isKindOfClass:[UIViewController class]]) {
        responder = [responder nextResponder];
        if (nil == responder) {
            break;
        }
    }
    UIViewController *viewController = (UIViewController *) responder;
    UIStoryboard *storyBoard = viewController.storyboard;
    

    【讨论】:

    • 我怎样才能得到名字??
    【解决方案3】:

    这样做,这可能会解决您的问题

    NSString *storyboardName;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        storyboardName = @"iPad";
    } else {
        storyboardName = @"iPhone";
    }
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    相关资源
    最近更新 更多