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.
I am facing the same issue on pageViewController, everytime i swipe my pageViewController it calls my delegate twice and am not able to find the issue. And my array is dynamic so i want the proper index value from pageController. First of all set the delegate of your pageViewController. Like this :-
self.pageViewController.delegate = self;
And then add this delegate method to your class
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if (completed) {
NSInteger currentIndex = ((PageContentViewController *)self.pageViewController.viewControllers.firstObject).pageIndex;
NSLog(@"%ld",(long)currentIndex);
}
}
PageContentViewController is the class where you are showing your data of pageViewController.
Run your project and if everything is working fine, you will get your index value. PageIndex is the Integer value for getting the current index. Hope it will helps you.
Thanks
Mandeep Singh
There is no nice way to do this but I think the most reliable way is to observe UIPageViewControllerDelegate
calls:
var index: Int
var proposedViewController: UIViewController?
// MARK: UIPageViewControllerDelegate
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard let proposedViewController = proposedViewController,
let index = myViewControllers?.index(of: proposedViewController) else {
return
}
self.index = index
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
guard pendingViewControllers.count == 1,
let proposedViewController = pendingViewControllers.first else {
return
}
self.proposedViewController = proposedViewController
}
Implement Delegate Method
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
NSLog(@"Current Page = %@", pageViewController.viewControllers);
UIViewController *currentView = [pageViewController.viewControllers objectAtIndex:0];
if ([currentView isKindOfClass:[FirstPageViewController class]]) {
NSLog(@"First View");
}
else if([currentView isKindOfClass:[SecondPageViewController class]]){
NSLog(@"Second View");
}
else if([currentView isKindOfClass:[ThirdViewController class]]){
NSLog(@"Third View");
}
}
//pageViewController.viewControllers always return current visible View ViewController
You can try this. This will give you current page.
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
UIViewController * viewController = [previousViewControllers lastObject];
NSUInteger previousPage = [(PageContentViewController *)viewController index];
NSUInteger currentPage = previousPage+1;
}
for swift:
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){
let pageContentViewController = pageViewController.viewControllers![0] as! ViewController
let index = pageContentViewController.pageIndex
}