Programmatically change rootViewController of storyBoard

IosUiviewcontrollerStoryboard

Ios Problem Overview


I have created my project using Storyboards. The root ViewController lies inside a Storyboard, I have not written a single code in the 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.

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.

    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:.

Ios Solutions


Solution 1 - Ios

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

Swift 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!

Solution 2 - Ios

enter image description here Set storyboard ID for your class in your main storyboard.

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;

Solution 3 - Ios

In swift we can implement it is as following

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

Solution 4 - Ios

I use simple this:

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

Solution 5 - Ios

Just to add to Sunny Shah's answer, this is the Swift 3 version of it:

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

Solution 6 - Ios

> Swift 3 code:

Use below in didFinishLaunchingWithOptions Appdelegate function. Replace "HomeViewController" with ViewController you want to set as Root ViewController on app launch.

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

Solution 7 - Ios

Swift 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!

Solution 8 - Ios

Objective c

step 1: remove main story board from info.plist

step 2: add storyboard id to your view controller in your interface builder

step 3: add the following code to application did finish method in app delegate

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];
}

Solution 9 - Ios

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.

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = self

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRanjitView Question on Stackoverflow
Solution 1 - IosSunny ShahView Answer on Stackoverflow
Solution 2 - IosMayank JainView Answer on Stackoverflow
Solution 3 - IosNischal HadaView Answer on Stackoverflow
Solution 4 - IosMUHAMMAD WASIMView Answer on Stackoverflow
Solution 5 - IosAldo GomezView Answer on Stackoverflow
Solution 6 - IosKaran DuaView Answer on Stackoverflow
Solution 7 - IosCarlos IranoView Answer on Stackoverflow
Solution 8 - IosDaniel RaoufView Answer on Stackoverflow
Solution 9 - IosWaFuNukeView Answer on Stackoverflow