[英]Programmatically change rootViewController of storyBoard
I have created my project using Storyboards
.我已经使用
Storyboards
创建了我的项目。 The root ViewController
lies inside a Storyboard
, I have not written a single code in the appDelegate
.根
ViewController
位于Storyboard
,我没有在appDelegate
编写任何代码。
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
.现在我想展示我的应用程序的游览,所以我想将根
ViewController
从Tab Bar
更改为我的 TourVC,当应用程序游览完成时,我想再次将我的根ViewController
切换回Tab Bar
。
So I looked up online and followed the following points于是上网查了一下,按照以下几点
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. 1) 从 app.plist 文件中删除
Storyboards
,2) 从Storyboards
取消选中选项“isInitialViewController”,在Tab Bar
控制器的情况下选中该Tab Bar
因为它是根ViewController
,3) 在 appDelegate.m 文件中添加此代码。
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
And also I get a warning,我也收到警告,
Unsupported Configuration: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:.
Objective-C:目标-C:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarcontroller"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
Swift :斯威夫特:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabBarcontroller") as UITabBarController
UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;
Swift 3:斯威夫特 3:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
Swift 5:斯威夫特 5:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
Or simply like this:或者简单地像这样:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
Both works fine!两者都工作正常!
Set storyboard ID for your class in your main storyboard.
在主故事板中为您的班级设置故事板 ID。
UIStoryboard *MainStoryboard = [UIStoryboard storyboardWithName:@"Main"
bundle: nil];
UINavigationController *controller = (UINavigationController*)[MainStoryboard
instantiateViewControllerWithIdentifier: @"RootNavigationController"];
LoginViewController *login=[MainStoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[controller setViewControllers:[NSArray arrayWithObject:login] animated:YES];
self.window.rootViewController=controller;
In swift we can implement it is as following在swift中我们可以实现它如下
let storyboard = UIStoryboard(name: "StartingPage", bundle: NSBundle.mainBundle())
let loginView: SignInVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInVC
UIApplication.sharedApplication().keyWindow?.rootViewController = loginView
I use simple this:我使用简单的这个:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NameOfStoryBoard" bundle:nil];
UITabBarController *rootViewController = [sb instantiateViewControllerWithIdentifier:@"NameOfTabBarController"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
Just to add to Sunny Shah's answer, this is the Swift 3 version of it:只是为了添加 Sunny Shah 的回答,这是它的 Swift 3 版本:
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController: UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
Swift 3 code:
斯威夫特 3 代码:
Use below in didFinishLaunchingWithOptions Appdelegate function.下面在 didFinishLaunchingWithOptions Appdelegate 函数中使用。 Replace "HomeViewController" with ViewController you want to set as Root ViewController on app launch.
将“HomeViewController”替换为您要在应用程序启动时设置为 Root ViewController 的 ViewController。
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
Objective c目标c
step 1: remove main story board from info.plist第 1 步:从 info.plist 中删除主故事板
step 2: add storyboard id to your view controller in your interface builder第 2 步:将故事板 ID 添加到界面构建器中的视图控制器
step 3: add the following code to application did finish method in app delegate第 3 步:将以下代码添加到应用程序委托中的应用程序完成方法中
self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
//set main story board
if( condition){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName1" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
[self window].rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}else{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName2" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
[self window].rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}
This is an old article, but I will reply.这是一篇旧文章,但我会回复。 I do not recommend the following code.
我不推荐以下代码。
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
Because you are creating two instances.因为您正在创建两个实例。 I recommend writing the following code in the appropriate ViewController.
我建议在适当的 ViewController 中编写以下代码。
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = self
Swift 5 + Xcode 11:斯威夫特 5 + Xcode 11:
Make like this:像这样:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
Or like this:或者像这样:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
Both works fine!两者都工作正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.