I have a weird problem with a UIPageViewController
. I use it to display several pages of data.
In case there is more than two pages, I let the user cycle through them, meaning he will see the first page again when scrolling further on the last page.
When I dismiss the pageController
it gets properly deallocated and I don't capture the single controllers that are displayed inside of it separately. Still when I open the pageController
again with a different dataset, I sometimes see one more page from the previous dataset.
To make my problem a little clearer, this is an example of what happens:
This is how my UIPageViewController
is setup:
let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
let controllers = dataModels.enumerated().map { index, dataModel in
let vc = DetailsController(model: dataModel)
vc.view.tag = index
return vc
}
pageViewController.delegate = self
pageViewController.dataSource = controllers.count > 1 ? self : nil
pageViewController.setViewControllers([controllers[0]], direction: .forward, animated: true, completion: nil)
So my question is: how can it be that the pageController
gets deallocated and still somehow captures old data when it is opened again later?
Found a fix. I went through everything related to UIPageViewController I could find here. The answer that led me to a fix was this one, even though the question is not directly related to my problem: https://stackoverflow.com/a/48574512/6518431
The second point in malex answer is the crucial one: if I call setViewControllers
without animation like this pageViewController.setViewControllers([controllers[0]], direction: .forward, animated: false, completion: nil)
, the problem does not appear anymore.
I can only imagine that there is some bug in the animation code of UIPageViewController, that keeps one of the displayed pages alive. Anyway, as I don't actually need to animate setting the initial controller, this works fine as a fix.