TabBarController -> NavigationController -> Multiple UIViewController (rootview controllers) using Storyboard

StackOverflow https://stackoverflow.com/questions/20727140

Question

I am developing an application using storyboard that has the following structure:

- TabBarController (2 Tab Bars) - Initial View Controller
    o NavigationController1
       • RegistrationPage - UIViewController (candidate for rootview)
       • DoActivityPage - UIViewController (candidate for rootview)

    o NavigationController2
       • View Controller 1

When first time app launches, I want to show RegistrationPage to the user and on subsequent times, user will be presented with DoActivityPage. Since, both these pages are at same level, each one of them is a potential candidate to be a root view controller.

I am using Storyboard layout, so visually I can only make one of the views as rootViewController, which doesn't serve my purpose. So, I know I will have to achieve that programatically and I have tried a lot on Google but couldn't find a way to do it.

My question is, how do I programmatically set root view controller (out of multiple rootview controllers) for navigation controller, keeping in mind that I am using storyboard?

Presently, I see a black page when the app launches with 1st tab item selected. If I add the following code in AppDelegate.m, i see the page, but empty bottom and top bars:

UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
RegistrationPage *rp = [navController.storyboard instantiateViewControllerWithIdentifier:@"RegistrationPage"];
navController.viewControllers = [NSArray arrayWithObjects: rp, nil];

@Michael I had tried similar code before, but the problem is since I am using storyboard, the only way I can access tabBarController is through this:

self.window.rootViewController.tabBarController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self.window.rootViewController.tabBarController];

And, when I tried to use the above code I get an error Application tried to push a nil view controller on target

Was it helpful?

Solution

Usually the registration/login view controller is not integrated inside the tab bar.
It is a completely separate view controller (may be contained in its own navigation controller).
Once the app is launched you should check in code if registration/login should be opened and open it as modal view controller.

EDIT
Assuming that you have initialised tabBarController property, registrationViewController property and isRegistered method that returns BOOL in your AppDelegate,
Put the next code inside your application:didFinishLaunchingWithOptions: method (right before return YES;):

if ([self isRegistered] == NO) {
    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:self.tabBarController];
    [self.tabBarController presentViewController:navController
                                        animated:NO
                                      completion:NULL];
}
  • There might be better place to put this code but this is the simplest to explain, assuming that I have no idea what classes you have in your project.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top