Programmatically set the initial view controller using Storyboards(使用 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.
在情节提要中,取消选中是初始视图控制器";来自第一个视图控制器的属性.
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:
无法为 UIMainStoryboardFile 'MainStoryboard' 实例化默认视图控制器 - 可能未设置指定的入口点?
Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?
您会注意到应用委托中的 window 属性现在为零.
And you'll notice that your window property in the app delegate is now nil.
在应用程序的设置中,转到您的目标和 Info
选项卡.Main storyboard file base name
的值是明确的.在 General
选项卡上,清除 Main Interface
的值.这将删除警告.
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.
在应用委托的 application: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 以编程方式设置初始视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Storyboard 以编程方式设置初始视图控制器


基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01