以编程方式在委托中调用storyboard

时间:2012-03-22 18:21:13

标签: objective-c ios xcode uiviewcontroller uistoryboard

我正在尝试以编程方式调用我的故事板。 我的故事板包含以下内容:

[导航控制器] - > [MainMenuView] - > [的DetailsView]

“MainMenu”标识符放在[MainMenuView]

我遇到的问题是屏幕显示为空白。我需要做什么?

感谢。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{       
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    MainMenuView *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainMenu"];

    return YES
}

2 个答案:

答案 0 :(得分:16)

您需要先手动创建窗口,然后在其中添加rootViewController(之前的答案几乎是正确的):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    UIViewController *mainViewController = [storyboard instantiateInitialViewController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = mainViewController;
    [self.window makeKeyAndVisible];

    return YES;
}

答案 1 :(得分:2)

您需要设置应用程序窗口的rootViewController属性:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{       
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    MainMenuView *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainMenu"];

    self.window.rootViewController = rootViewController;

    return YES;
}