添加为subView时,未调用UIViewController viewWillAppear [英] UIViewController viewWillAppear not called when adding as subView

查看:87
本文介绍了添加为subView时,未调用UIViewController viewWillAppear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController ,我从另一个视图控制器中加载,然后将其视图添加到 UIScrollView

I have a UIViewController that I am loading from inside another view controller and then adding its view to a UIScrollView.

self.statisticsController = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
self.statisticsController.match = self.match;

[self.scrollView addSubview:self.statisticsController.view];

我在统计视图控制器和 viewDidLoad ,但 viewWillAppear 不是。

I've put breakpoints in the statistics view controller and viewDidLoad is being called but viewWillAppear isn't.

是不是因为我没有推动它在层次结构上还是什么?

Is it because I'm not pushing it onto the hierarchy or something?

推荐答案

你应该添加 statisticsController 作为控制器的子视图控制器,您将其视图添加到其中。

You should add statisticsController as a child view controller of the controller whose view you're adding it to.

self.statisticsController = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
self.statisticsController.match = self.match;

[self.scrollView addSubview:self.statisticsController.view];
[self addChildViewController:self.statisticsController];
[self.statisticsController didMoveToParentViewController:self];

我不确定这会使 viewDidAppear 调用,但是你可以在子控制器中覆盖 didMoveToParentViewController:,并且会调用它,这样你就可以放入任何你输入的代码 viewDidAppear 在那里。

I'm not sure this will make viewDidAppear get called, but you can override didMoveToParentViewController: in the child controller, and that will be called, so you can put any code that you would have put in viewDidAppear in there.

这篇关于添加为subView时,未调用UIViewController viewWillAppear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆