如何以编程方式为 Storyboard 设置 InitialViewController
?我想根据某些情况打开我的 Storyboard到不同的 View ,这可能会因发布而异。
最佳答案
如何没有虚拟初始 View Controller
确保所有初始 View Controller 都有 Storyboard ID。
在 Storyboard 中,取消选中第一个 View Controller 中的“是初始 View Controller ”属性。
如果您此时运行应用程序 you'll read :
Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?
您会注意到应用委托(delegate)中的 window 属性现在为零。
在应用程序的设置中,转到您的目标和 Info
选项卡。 Main storyboard file base name
的值是明确的。在 General
选项卡上,清除 Main Interface
的值。这将删除警告。
在应用委托(delegate)的 application:didFinishLaunchingWithOptions:
方法中创建窗口和所需的初始 View Controller :
- (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;
}
关于ios - 使用 Storyboard 以编程方式设置初始 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428629/