使用Storyboard以编程方式设置初始视图控制器 [英] Programmatically set the initial view controller using Storyboards

查看:311
本文介绍了使用Storyboard以编程方式设置初始视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式为Storyboard设置 InitialViewController ?我想根据一些条件打开我的故事板到不同的视图,这些条件可能因发布而异。

How do I programmatically set the InitialViewController for a Storyboard? I want to open my storyboard to a different view depending on some condition which may vary from launch to launch.

推荐答案

如何没有虚拟初始视图控制器

How to without a dummy initial view controller

确保所有初始视图控制器都有一个故事板ID。

Ensure all initial view controllers have a Storyboard ID.

在故事板中,取消选中第一个视图控制器中的Is Initial View Controller属性。

In the storyboard, uncheck the "Is initial View Controller" attribute from the first view controller.

如果您在此处运行应用程序你会读到:

If you run your app at this point you'll read:

Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?

您会注意到app delegate中的窗口属性现在为零。

And you'll notice that your window property in the app delegate is now nil.

在应用程序的设置中,转到目标和信息标签。清除主故事板文件基本名称的值。在常规标签上,清除主界面的值。这将删除警告。

In the app's setting, go to your target and the Info tab. There clear the value of Main storyboard file base name. On the General tab, clear the value for Main Interface. This will remove the warning.

在app delegate的应用程序中创建窗口和所需的初始视图控制器:didFinishLaunchingWithOptions:方法:

Create the window and desired initial view controller in the app delegate's application:didFinishLaunchingWithOptions: method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

这篇关于使用Storyboard以编程方式设置初始视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆