Question

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?

Was it helpful?

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.

OTHER TIPS

At a time there is only one rootviewController in app,

you can replace directly using following,

   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    YourVC *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"YourVC"];
    self.window.rootViewController = rootViewController;

in appDelegate Method,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top