将子控制器添加到表视图单元格时,不会调用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生命线函数
- 重新创buildiOS 7日历UIViewanimation
- 框架添加到cocoapod getiing导入问题“框架未find”
- 在更新UITableView中显示的CoreData模型后,应用程序崩溃
- 如何使我的AFNetworking“responseObject”得到我可以解析的NSDictionary?
- 界面生成器中的图像和标签与TableView单元格中的数据重叠
- 当webview大于屏幕时,将UIWebview内容转换为UIImage
- 在iOS中处理后台任务的正确方法是什么?
- WooCommerce iOS OAuth无效签名
- iOS:Siri不可用不会返回AVAudioSessionInterruptionOptionShouldResume