Programmatically call storyboard in delegate

2019-03-27 17:31发布

I'm trying to programmatically call my storyboard. My storyboard consists of the following:

[Navigation Controller] -> [MainMenuView] -> [DetailsView]

The "MainMenu" identifier was placed in the [MainMenuView]

The problem i'm having is the screen showing blank. What do i need to do?

Thanks.

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

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

    return YES
}

2条回答
倾城 Initia
2楼-- · 2019-03-27 18:18

You need to set the rootViewController property of your application's window:

- (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;
}
查看更多
地球回转人心会变
3楼-- · 2019-03-27 18:21

You need to first create the window manually and then add the rootViewController in it (the previous answer was almost correct):

- (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;
}
查看更多
登录 后发表回答