我有一个 UIViewController
,我从另一个 View Controller 中加载它,然后将其 View 添加到 UIScrollView
。
self.statisticsController = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
self.statisticsController.match = self.match;
[self.scrollView addSubview:self.statisticsController.view];
我在统计 View Controller 中放置了断点,viewDidLoad
被调用,但 viewWillAppear
没有被调用。
是因为我没有把它推到层次结构上还是什么?
最佳答案
您应该将 statisticsController
添加为要将其添加到其 View 的 Controller 的 subview Controller 。
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
被调用,但是你可以覆盖子 Controller 中的 didMoveToParentViewController:
,它会被调用,所以你可以把您将在 viewDidAppear
中放入的任何代码。
关于ios - 添加为 subview 时未调用 UIViewController viewWillAppear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18235284/