以编程方式更改 storyBoard 的 rootViewController [英] Programmatically change rootViewController of storyBoard

查看:25
本文介绍了以编程方式更改 storyBoard 的 rootViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Storyboards 创建了我的项目.根 ViewController 位于 Storyboard 内,我没有在 appDelegate 中编写任何代码.

I have created my project using Storyboards. The root ViewController lies inside a Storyboard, I have not written a single code in the appDelegate.

现在我想展示我的应用程序的导览,所以我想将根 ViewControllerTab Bar 更改为我的 TourVC,并且当应用程序导览是完成,我想再次将我的根 ViewController 切换回 Tab Bar.

Now I want to show a tour of my app, so I want to change the root ViewController from Tab Bar to my TourVC and when the tour of the app is finished , I want to again switch back my root ViewController to Tab Bar.

于是上网查了一下,按照以下几点

So I looked up online and followed the following points

1) 从 app.plist 文件中删除 Storyboards,2) 取消选中 Storyboards 中的选项isInitialViewController",在 Tab Bar 控制器的情况下选中该选项,因为它是根 ViewController,3) 将此代码添加到 appDelegate.m 文件中.

1) Remove Storyboards from app.plist file, 2) Uncheck option "isInitialViewController" from Storyboards which is checked in case of Tab Bar controller because its a root ViewController, 3) Add this code in appDelegate.m file.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ProductTourViewController *PT = [[ProductTourViewController alloc] initWithNibName:@"ProductTourViewController" bundle:nil];
self.window.rootViewController = PT;
[self.window makeKeyAndVisible];
return YES;

但我的应用程序崩溃并显示此错误日志,

But my app crashes with this error log,

[ProductTourViewController selectedViewController]: unrecognized selector sent to instance 0x1766a9e0

我也收到了警告,

Unsupported Configuration: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:.

推荐答案

Objective-C:

Objective-C:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarcontroller"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];

斯威夫特:

 let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabBarcontroller") as UITabBarController  
   UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;

斯威夫特 3:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController

Swift 5:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()

或者简单地像这样:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()

两者都很好!

这篇关于以编程方式更改 storyBoard 的 rootViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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