Search code examples
objective-cuiviewcontrollerios7xcode5rootview

How can you add 2 rootviewcontrollers in IOS App using storyboard


I am curious to know if i can develop an app having 2 view controllers .I have gone through some links ,but couldn't find a solution if I'm using storyboard.If i already have a rooviewcontroller,how can i remove it and add another view as rootviewcontroller?Any thoughts?


Solution

  • You can do so. You just have to add the code below to the place/action where you want to change the rootViewController.

    //First dismiss your currently loaded ViewController
    [self dismissViewControllerAnimated:YES completion:nil];
    
    //Get the keyWindow of the app
    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    
    NSString *identifier = @"Your_Identifier_Name_For_ViewController";// this is the identifier name(Storyboard ID)
                                                                      // of the AnotherRootViewController
                                                                      // which you have to set in your Storyboard
                                                                      // as shown in the figure.
    
    //Now create an object of the AnotherRootViewController
    AnotherRootViewController *newRootViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
    
    //Finally set your newRootViewController
    [window setRootViewController:newRootViewController];
    

    And make setting of AnotherRootViewController to Storyboard as shown in figure:

    enter image description here

    Let me know if it satisfy your requirement.