在运行时获取主故事板

时间:2022-04-25 20:01:13

I'd like to be able to simply call [UIStoryboard mainStoryboard] to get either the iPad or iPhone storyboard at runtime.

我想可以简单地调用[uistoryboardmainstoryboard]在运行时获得iPad或iPhone故事板。

2 个解决方案

#1


21  

Here's a UIStoryboard category that'll do just this:

这是一个uistoryboardcategory它会这样做:

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

Here's a link to the gist

这是要点的链接

#2


12  

You can use [UIStoryboard storyboardWithName:storyboardName bundle:bundle] if the storyboard has not already been loaded.

如果故事板还没有加载,你可以使用[uistoryboardwithname:storyboardName bundle:bundle]。

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.

你也可以使用viewController。获取已有的故事板。如果您有一个主窗口作为应用程序委托的一部分(您可能有),您可以获得rootViewController。故事板。

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

#1


21  

Here's a UIStoryboard category that'll do just this:

这是一个uistoryboardcategory它会这样做:

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

Here's a link to the gist

这是要点的链接

#2


12  

You can use [UIStoryboard storyboardWithName:storyboardName bundle:bundle] if the storyboard has not already been loaded.

如果故事板还没有加载,你可以使用[uistoryboardwithname:storyboardName bundle:bundle]。

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.

你也可以使用viewController。获取已有的故事板。如果您有一个主窗口作为应用程序委托的一部分(您可能有),您可以获得rootViewController。故事板。

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