https://github.com/soberZhichao/AddChildVC
- (IBAction)leftClick:(id)sender
{
[self didRemoveCurrentVC];
FirstViewController *firstVC = [[FirstViewController alloc] init];
self.currentVC = firstVC;
[self addChildVC];
}
- (IBAction)rightClick:(id)sender
{
[self didRemoveCurrentVC];
SecondViewController *secondVC = [[SecondViewController alloc] init];
self.currentVC = secondVC;
[self addChildVC];
}
-(void)addChildVC
{
[self addChildViewController:self.currentVC];
//addChildViewController 会调用 [child willMoveToParentViewController:self] 方法,但是不会调用 didMoveToParentViewController:方法,官方建议显示调用
[self.currentVC didMoveToParentViewController:self];
self.currentVC.view.frame = CGRectMake(0, 100, 400, 300);
[self.view addSubview:self.currentVC.view];
}
- (void)didRemoveCurrentVC
{
if (self.currentVC)
{
[self.currentVC.view removeFromSuperview];
[self.currentVC removeFromParentViewController];
}
}