ios - 添加为 subview 时未调用 UIViewController viewWillAppear

标签 ios objective-c uiviewcontroller uiscrollview viewwillappear

我有一个 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/

相关文章:

ios - 如何显示当前日期与过去日期之间的差异?

objective-c - 委托(delegate)、 socket 和鼠标事件的类设计

ios - 真的很奇怪的应用程序颜色行为?

ios - Objective-C:应用程序范围内完全可重用的自定义 UITableViewCell

ios - 在 UINavigationController 中设置框架 View Controller View

ios - UITableView 删除/添加行导致 CoreData : Serious Application Error if another object has been selected in the MasterView of a SplitViewController

ios - 构造具有元类型值的类类型 'ClassName' 的对象必须使用 'required' 初始化程序 XCode8 Swift 3 更改

ios - iOS 7 中的新图标

ios - UILabel 文本在应用缩放变换时被压缩/拉伸(stretch)

ios - 设置另一个 UIViewController 的属性