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?
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.
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.
精彩评论