I have been struggling with this issue for the last few days and after all this juggling I have figured out that all I need is the current Index from the datasource method to up
My approach is quite simple:
When the page (controller) is instantiated I pass in the page index, which at this point is known.
Then in the delegate call below, iff the transition finished, I obtain the controller currently shown and cast it to my custom page class, before I fetch and update the currentPage with the associated page index.
To keep things simple I setup the data source and delegate to be the WalkThroughController
which embeds the page view controller.
extension WalkThroughController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard finished, let currentPageController = pageViewController.viewControllers?.last as? WalkThroughPageViewController else {
return
}
self.currentPage = currentPageController.pageIndex
}
}
The take away point is to set the page index of the pages when they are created in the data source. With this approach it is very straight forward to retrieve it later.