将子控制器添加到表视图单元格时,不会调用viewWillAppear

当我添加子视图控制器到表视图单元格,它看起来像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); }]; } 

任何想法,为什么发生?

 - (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]; 

为了@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生命线函数