ios - 在运行时获取主 Storyboard

标签 ios objective-c cocoa-touch uistoryboard

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

最佳答案

这是一个 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

关于ios - 在运行时获取主 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19327646/

相关文章:

objective-c - UIResponder 链题

ios - 将一个 View 动画化为不同层次结构中的另一个 View

iphone - GCD : How to change timer fire interval

ios - 停止由 `performSelector` 在 `viewDidDisappear` 上启动的子线程

objective-c - 离线数据存储/同步选项、Core Data、TouchDB 或其他?

objective-c - 为什么 NSRect 和 CGRect 是不兼容的类型?

objective-c - 如何使用 NSPredicate 捕获子对象?

objective-c - 在 objective-c 中返回错误

ios - 快速保存用户数据并使用数据做出决定(iOS app dev)

ios - 错误 : Initializers may only be declared within a type