当我将子视图控制器添加到表视图单元格时,看起来没有调用子视图控制器的 viewWillAppear ,只有 viewDidAppear 。
表视图控制器方法:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("ShopInfoTableViewCell", forIndexPath: indexPath) as! ShopInfoTableViewCell
self.addChildViewController(self.shopInfoViewController, toView: cell.containerView)
return cell
}
查看控制器类别方法:
- (void)addChildViewController:(UIViewController *)childController toView:(UIView *)view
{
[self addChildViewController:childController];
[view addSubview:childController.view];
[childController didMoveToParentViewController:self];
[childController.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(view.mas_top);
make.bottom.equalTo(view.mas_bottom);
make.left.equalTo(view.mas_left);
make.right.equalTo(view.mas_right);
}];
}
任何想法为什么会发生?
答案 0 :(得分:21)
- (void)addChildViewController:(UIViewController *)childController toView:(UIView *)view
{
[self addChildViewController:childController];
//add this
[childController beginAppearanceTransition:YES animated:YES];
[view addSubview:childController.view];
[childController endAppearanceTransition];
[childController didMoveToParentViewController:self];
[childController.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(view.mas_top);
make.bottom.equalTo(view.mas_bottom);
make.left.equalTo(view.mas_left);
make.right.equalTo(view.mas_right);
}];
}
同样,当你想要消失时,你应该调用它
[childController beginAppearanceTransition:NO animated:YES];
[childController.view removeFromSuperview];
[childController endAppearanceTransition];
答案 1 :(得分:-1)
要@adali回答我会改变:
[childController beginAppearanceTransition:YES animated:YES];
由:
[childController willMoveToParentViewController:self];
所以最后会是:
[self addChildViewController:childController]; //add the child on childViewControllers array
[childController willMoveToParentViewController:self]; //viewWillAppear on childViewController
[self.containerView addSubview:childController.view]; //add childView whenever you want
[childController didMoveToParentViewController:self]; //viewDidAppear on childViewController
非常重要,一旦包含子项的viewController已经执行了viewWillAppear life cicle函数,就必须调用所有这些