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?
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.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.
if you implement ModelController (as in Apple example), use it's method indexOfViewController
I have solved this problem like this:
* Parent View Controller (PageViewController) *
* Child View Controller *
For example, the parentPageViewController's currentPageIndex is 10 , or some other values, when your swipe right, parentViewController.currentPageIndex will be 9, if 0 , nothing happened; If you swipe left, parentPageViewController.currentPageIndex will be 11, if the showing controller.view is the last, nothing happened too.
This is how I determine the current page index whether the user swipes to the left or to the right and whether he changes his swipe direction gesture in the middle of swiping. Just make sure you have a pageIndex property in your view controller to store the index:
You can try this. This will give you current page.
To get the current index you'll have to do the following: