nornagon
nornagon

Reputation: 15821

How does UITabBarController work?

I'm making a custom thing that should work similarly to UITabBarController—a UIViewController that manages sub-ViewControllers. The parent UIViewController should manage adding and removing the views of the child UIViewControllers.

It seems if I'm doing things this way I need to call [UIViewController viewWillAppear] and friends manually. Is this the case? Is there a better way to do this?

Upvotes: 1

Views: 788

Answers (2)

Marcos Crispino
Marcos Crispino

Reputation: 8218

Why are you doing this? Why not use UITabBarController?

Anyway... I would try with something like this

- (void) selectViewControllerAtIndex:(int)index {
    [[self.viewControllers objectAtIndex:self.selectedVCIndex].view removeFromSuperview];
    self.selectedVCIndex = index;
    [self.view addSubView:[self.viewControllers objectAtIndex:index].view];
    [self.view setNeedsLayout];
}

Never tried it, though.

Upvotes: 1

Dunja Lalic
Dunja Lalic

Reputation: 752

Maybe you want to create a singleton with these methods:

@interface RootViewController : UIViewController {

    UIViewController *currentVC;

}

-(void) destroyCurrent;
-(void) switchToViewController: (int) controller;

-(void) switchToViewController: (int) controller {

    [self destroyCurrent];

    switch (controller) {
        case 0:
            MyViewController *viewController = [[MyViewController alloc] init];
            currentVC = viewController;
            break;
        //case 1: ...
        default:
            break;
    }

    [self.view addSubview:currentVC.view];
}

-(void) destroyCurrent {

    if (currentVC) {
        [currentVC.view removeFromSuperview];
        [currentVC release];
        currentVC = nil;
    }
}

where by calling switchToViewController: (int) controller you change the view controllers.

Upvotes: 1

Related Questions