Update current page number after pageviewcontrolle

2019-09-09 20:19发布

问题:

Problem having is that it start updating current page number after you turn 3 or 4 pages over then it start updating current page number starting from 1, 2, 3, 4,..... which is wrong. and if you start in reverse order turning page over lets suppose from 12 then it shows 13 and then from next page it starts updating current pagenumber in reverse order.

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{


if (!completed) {

    return;
}
  if (currentIndex<totalPages -1 ){

  [self displaycurrentIndex:currentIndex + 1];

     }else if (currentIndex>totalPages + 1){

          [self displaycurrentIndex:currentIndex - 1];   
     } 
 }

- (void) displaycurrentIndex:(NSUInteger)currentIndex {

self.navigationItem.title = [NSString stringWithFormat:
                             @"Page %i of %i",
                            currentIndex + 1,
                             CGPDFDocumentGetNumberOfPages(PDFDocument)];
}

Please help in how to fix this updating current page number from the beginning and also when going in reverse order.

Edit:

After removing

if (!completed) {

return;
}

Now it is Updating after turning first page over but now updates current page number from 0 of 20 and then 1 of 20 and so on but still when turning page over in reverse order it still updates current pagenumber in forward and then in reverse order.

Thanks for help.

回答1:

From my experience (and also it is confirmed in the past WWDC session videos dedicated to this class) the best way to manage the page numbers in a UIPageViewController is to store as a property the page number information directly in the page view controller (that is in the view controller that contains the single page you want to show).

Then if you want to show a general purpose status bar with the page number, you simply fetch the viewControllers property of the UIPageViewController, which returns one or two view controllers depending on the UIPageViewController setup.

So in pseudo-code when the page view controller asks its data source which are the new view controllers to display your code: - will get the current view controller(s) - will fetch from it (them) the page number - will calculate the next or prev page number(s) (depends on the page curl/swipe direction) - will ask to the data source for the new calculated page numbers view controllers and will assign to their page number property the new value

In this way you are protected from strange animation behaviours and you don't need to keep in memory a global "currentIndex" which may lead to issues, as you experienced.