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 會(huì)調(diào)用 [child willMoveToParentViewController:self] 方法沙廉,但是不會(huì)調(diào)用 didMoveToParentViewController:方法,官方建議顯示調(diào)用
[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];
}
}