添加为子视图时未调用 UIViewController viewWillAppear [英] UIViewController viewWillAppear not called when adding as subView
问题描述
我有一个 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.
这篇关于添加为子视图时未调用 UIViewController viewWillAppear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!