以编程方式更改storyBoard的rootViewController

我用Storyboards创build了我的项目。 根ViewController位于Storyboard ,我没有在appDelegate编写单个代码。

现在,我想显示我的应用程序的一个浏览,所以我想要从根目录Tab Bar更改根视图控制器到我的TourVC,当应用程序的巡视完成后,我想再次切换回我的根视图控制器到Tab Bar

所以我在网上查了一下,跟着以下几点

1)从app.plist文件中删除Storyboards ,2)取消选中Storyboards选项“isInitialViewController”,在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; 

但我的应用程序崩溃与此错误日志,

 [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的:

 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:

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

在这里输入图像说明 在您的主故事板中为您的课程设置故事板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; 

在swift中我们可以实现它如下

 let storyboard = UIStoryboard(name: "StartingPage", bundle: NSBundle.mainBundle()) let loginView: SignInVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInVC UIApplication.sharedApplication().keyWindow?.rootViewController = loginView 

我使用简单的这个:

 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NameOfStoryBoard" bundle:nil]; UITabBarController *rootViewController = [sb instantiateViewControllerWithIdentifier:@"NameOfTabBarController"]; [[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController]; 

为了增加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代码:

在下面的didFinishLaunchingWithOptions Appdelegate函数中使用。 将“HomeViewController”replace为您想在应用启动时设置为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