简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM