1

First question on S/O, so apologies if I haven't followed any protocols. I have been trying to display conditionally 4 different types of scene using 4 UIViewControllers from the same UITabBar item. It has to be the same item on the TabBar, because the UIViewController used is dependent upon data rather than user selection.

I know that subclassing UITabBarController is not advised, so I have set up a UIViewController that handles the conditional selection of the required scene (see code below). This works well, but despite trying everything I can think of, fails to show the TabBar at the bottom of the newly selected view. I have also tried using a UINavigation controller, but this was less successful. In Storyboard, I have tried all the different permutations of setting the view sizes and presentation styles, change the Bottom Bar to Tab Bar in simulated metrics and tried using Segues. None have produced the requisite outcome.

- (void)viewDidLoad
{
    [super viewDidLoad];

    int m = 1;

    UIViewController *viewController;
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 ) {
        viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M31TVC"];
    }else if (m == 4 || m == 6 || m == 9 || m == 11 ){
        viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M30TVC"];
    }else if (m == 13 ){
        viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M29TVC"];
    }else if (m == 2 ){
        viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M28TVC"];
    }else {
        // error code here
    }
    viewController.hidesBottomBarWhenPushed = NO;
    [self presentViewController:viewController animated:NO completion:nil];


}

Thank you in advance to anyone who knows how to do this thing.

4

1 に答える 1

1

Instead of trying to replace to controller that will be shown to the user, you should only replace its view. That means you should implement a Container View Controller:

  • Create your UITabBarController and make one of its items a UIViewController
  • Make that into a custom controller and add the following properties to it.

    @property (nonatomic, strong) IBOutlet UIView *currentView
    @property (nonatomic, strong) UIViewController *currentViewController
    
  • Add a UIView to the controller in the storyboard and connect it to the *currentView outlet.

Now, everytime the user reaches this point in your app, you are going to do the following:

UIViewController *viewController;
if (month == 1 || 3 || 5 || 7 || 8 || 10 || 12 ) {
    viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M31TVC"];
}else if (month == 4 || 6 || 9 || 11 ){
    viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M30TVC"];
}else if (month == 13 ){
    viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M29TVC"];
}else if (month == 2 ){
    viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"M28TVC"];
}else {
    // error code here
}

[self addChildViewController:viewController];
[viewController didMoveToParentViewController:self];

if (self.currentViewController){
    [self.currentViewController willMoveToParentViewController:nil];

    [self transitionFromViewController:self.currentViewController toViewController:viewController duration:0 options:UIViewAnimationOptionTransitionNone animations:^{
        [self.currentViewController.view removeFromSuperview];
        [self.currentView addSubview:viewController.view];
    } completion:^(BOOL finished) {
        [self.currentViewController removeFromParentViewController];
        self.currentViewController = viewController;
    }];
} else {
    [self.currentView addSubview:viewController.view];
    self.currentViewController = viewController;
}

This is what I did on my app to create a custom tabbed controller, instead of using the default UITabBarController.

于 2012-09-25T13:54:51.050 に答える