UIViewControllers 在 UIPageViewController 中被重用

UIViewControllers being reused in UIPageViewController

我有一个 UIPageVIewController,我正在使用重用标识符初始化内部控制器,即:

BaseContentViewController *contentViewController = (BaseContentViewController *)[self.storyboard instantiateViewControllerWithIdentifier:self.controllerRestorationIDs[index]];

我注意到

if a UIViewController is reused then viewDidLoad is not called in that controller so any code that I want to be executed every single time the UIViewController appears won't.

例如,我想在每次出现时更改背景颜色。

有没有像 UITableViewCellprepareForReuse 那样的方法,我可以把这段代码放进去?

我会把它放在 -viewWillAppear:.

有一个类似prepareForReuse的方法,它是-viewWillAppear:: 如果你想在每次 ViewController 出现时改变背景颜色,你可以 setBackgroudColor 如:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.f
                                                    green:arc4random_uniform(256)/255.f
                                                     blue:arc4random_uniform(256)/255.f alpha:1.f
                                ];
}