我可以将 rootViewController 更改为 ViewController 中的不同故事板视图吗?

Can I change the rootViewController to a different storyboard view inside ViewController?

我有一些逻辑要求在按下 button 后出现不同的 view。以下代码适用于 appDelegate 但不适用于我的 ViewController 实现。有没有办法从当前 ViewController 实现更改视图控制器故事板?

self.window.rootViewController =
(UIViewController *)[[UIStoryboard storyboardWithName:@"Main" bundle: nil] instantiateViewControllerWithIdentifier:@"homeView"];

随着控制器的视图当前在屏幕上,它有一个非零 window 属性 指向它的 window。此外,如果使用此代码的控制器是在同一个故事板中制作的,您可以只使用 self.storyboard 而不是从其名称获取故事板。

    self.view.window.rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"homeView"];

您可以从 UIApplication 实例中获取 window

UIApplication *application = [UIApplication sharedApplication];
UIViewController *newRoot = [self.storyboard instantiateViewControllerWithIdentifier:@"homeView"];
[(AppDelegate *)application.delegate window].rootViewController = newRoot;