单纯的addChildViewController:,在点击tab切换时并没有移除controller和它的子view,仅需添加如下代码即可解决viewWillAppear:方法不响应的问题:
//Add Child Controller
[self addChildViewController:childController];
[childController didMoveToParentViewController:self];
[childController beginAppearanceTransition:YES animated:NO];
[self.view addSubview:childController.view];
[childController endAppearanceTransition];
如果仅需响应viewWillAppear:,以上代码即可实现。
扩展:移除子控制器
//Remove Child Controller
if([childController parentViewController]) {
[childController willMoveToParentViewController:nil];
[childController beginAppearanceTransition:NO animated:NO];
[childController.view removeFromSuperview];
[childController endAppearanceTransition];
[childController removeFromParentViewController];
}