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 update with current visible page number
I have this UIPageViewController
datasource method and I need to use the current index to get the current visible page for delegate method previousViewControllers transitionCompleted:(BOOL)completed
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];
NSURL *pdfurl = [NSURL fileURLWithPath:path];
PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
contentViewController = [[ContentViewController alloc] initWithPDFAtPath:path];
currentIndex = [modelArray indexOfObject:[(ContentViewController *)viewController page]];
if (currentIndex == totalPages - 1) {
return nil;
}
contentViewController.page = [modelArray objectAtIndex:currentIndex + 1];
return contentViewController;
}
I'm confused about how to write the current index statement from datasource method into delegate method to update current visible page.
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
if (!completed)
{
return;
}
//currentIndex = [[self.pageViewController.viewController lastObject]];
currentIndex = [self indexOfObject:contentViewController];
[self displaycurrentIndex:currentIndex];
//self.pageControl.currentPage = currentIndex;
}
How can I correct this?
I struggled with this too and none of the other solutions worked for me, but was finally able to solve it using a delegate method:
First in PageContentViewController.h Add the following property:
Second create a delegate protocol in the PageContentViewController (not the PageViewController).
In PageContentViewController.h:
Third in PageContentViewController.m perform delegate method in viewDidAppear
Fourth, in PageViewController.m implement the delegate method
Fifth, in PageViewController.m make sure you pass the pageIndex value and set the delegate
It looks like you should be able to get the current index with:
There is no nice way to do this but I think the most reliable way is to observe
UIPageViewControllerDelegate
calls:for swift:
Implement Delegate Method
//pageViewController.viewControllers always return current visible View ViewController
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 :-
And then add this delegate method to your class
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