以编程方式设置初始视图控制器使用故事板(Programmatically set the initial view controller using Storyboards)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
以编程方式设置初始视图控制器使用故事板(Programmatically set the initial view controller using Storyboards)

如何以编程方式设置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.

最满意答案

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

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

在故事板中,从第一个视图控制器中取消选中“是初始视图控制器”属性。

如果你现在运行你的应用程序,你会看到:

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

你会注意到你的应用程序委托中的窗口属性现在为零。

在应用的设置中,转到您的目标和Info选项卡。 显示Main storyboard file base name的价值。 在“ General选项卡上,清除“ Main Interface的值。 这将删除警告。

在应用程序委托application:didFinishLaunchingWithOptions:创建窗口和所需的初始视图控制器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; }

How to without a dummy initial view controller

Ensure all initial view controllers have a Storyboard ID.

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?

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.

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; }

更多推荐

本文发布于:2023-04-12 20:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/190921dd5b5f455f9a015e6b648b94bf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   控制器   方式   故事   Programmatically

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!