ios – UIViewController viewWillAppear添加为subView时不会调用

我有一个UIViewController,我从另一个视图控制器中加载,然后将其视图添加到UIScrollView.
self.statisticsController = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
self.statisticsController.match = self.match;

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

我在统计视图控制器中放置了断点,并且正在调用viewDidLoad,但是viewWillAppear不是.

是因为我没有把它推到层次结构上吗?

解决方法

您应该将statisticsController添加为控制器的子视图控制器,该控件的视图要添加到.
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中的任何代码放在那里.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...