ios 以编程方式更改 storyBoard 的 rootViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22653993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically change rootViewController of storyBoard
提问by Ranjit
I have created my project using Storyboards. The root ViewControllerlies inside a Storyboard, I have not written a single code in the appDelegate.
我已经使用Storyboards. 根ViewController在一个里面Storyboard,我没有在里面写过一个代码appDelegate。
Now I want to show a tour of my app, so I want to change the root ViewControllerfrom Tab Barto my TourVC and when the tour of the app is finished , I want to again switch back my root ViewControllerto Tab Bar.
现在,我想告诉我的应用程序的巡演,所以我想根本改变ViewController从Tab Bar我TourVC,当应用程序的游览结束后,我想我的根再次切换回ViewController至Tab Bar。
So I looked up online and followed the following points
于是上网查了一下,按照以下几点
1) Remove Storyboardsfrom app.plist file,
2) Uncheck option "isInitialViewController" from Storyboardswhich is checked in case of Tab Barcontroller because its a root ViewController,
3) Add this code in appDelegate.m file.
1)Storyboards从 app.plist 文件中删除,2) 取消选中“isInitialViewController”选项Storyboards,在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:.
回答by Sunny Shah
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
回答by Mayank Jain
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;
回答by Nischal Hada
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
回答by MUHAMMAD WASIM
I use simple this:
我使用简单的这个:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NameOfStoryBoard" bundle:nil];
UITabBarController *rootViewController = [sb instantiateViewControllerWithIdentifier:@"NameOfTabBarController"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
回答by Aldo Gomez
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
回答by Karan Dua
Swift 3 code:
斯威夫特 3 代码:
Use below in didFinishLaunchingWithOptions Appdelegate function. Replace "HomeViewController" with ViewController you want to set as Root ViewController on app launch.
下面在 didFinishLaunchingWithOptions Appdelegate 函数中使用。将“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
回答by Daniel Raouf
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];
}
回答by WaFuNuke
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

