UIPageViewController: get the currentPage

2019-03-14 17:25发布

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?

12条回答
2楼-- · 2019-03-14 17:57

My approach is quite simple:

  1. When the page (controller) is instantiated I pass in the page index, which at this point is known.

  2. 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.

查看更多
孤傲高冷的网名
3楼-- · 2019-03-14 18:04

if you implement ModelController (as in Apple example), use it's method indexOfViewController

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    myDataViewController *currentView = [pageViewController.viewControllers objectAtIndex:0];
    NSInteger currentIndex = [_modelController indexOfViewController:currentView];
    [self displayPageNumber:currentIndex];
}
查看更多
孤傲高冷的网名
4楼-- · 2019-03-14 18:07

I have solved this problem like this:

* Parent View Controller (PageViewController) *

@interface MyParentPageViewController () <UIPageViewControllerDataSource>


@property (nonatomic, assign) NSUInteger currentPageIndex;

@end

@implementation MyParentPageViewController

- (void)viewDidLoad 
{
  [super viewDidLoad];

  MyChildViewController *rootChildViewController = [MyChildViewController controllerWithPageIndex:self.currentPageIndex];
  [self setViewControllers:@[rootChildViewController]
             direction:UIPageViewControllerNavigationDirectionForward
              animated:YES completion:nil];
  self.dataSource = self;
}

#pragma mark - Page View Controller DataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
viewControllerAfterViewController:(MyChildViewController *)viewController
{
  if (viewController.pageIndex == self.sourceArray.count - 1) {
    return nil;
  }
  MyChildViewController *vc = [MyChildViewController controllerWithPageIndex:viewController.pageIndex + 1];
  return vc;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(CHHomeDetailTableController *)viewController 
{
  if (viewController.pageIndex == 0) {
    return nil;
  }
  MyChilViewController *vc = [MyChildViewController controllerWithPageIndex:viewController.pageIndex - 1];
  return vc;
}


@end

* Child View Controller *

@implementation MyChildViewController
- (void)viewWillAppear
{
    MyParentPageViewController *parentPageViewController = (MyParentPageViewController *)self.parentViewController;
    parentViewController.currentPageIndex = self.pageIndex;
}
@end

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.

查看更多
5楼-- · 2019-03-14 18:07

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:

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        let currentPageIndex = (pendingViewControllers.last! as! ViewController).pageIndex!
    }
查看更多
ゆ 、 Hurt°
6楼-- · 2019-03-14 18:11

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;
}
查看更多
7楼-- · 2019-03-14 18:13

To get the current index you'll have to do the following:

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


   NSUInteger index = [self.viewControllers indexOfObject: [pageViewController.viewControllers lastObject]];

   NSLog(@"INDEX: %lu",(unsigned long)index);

}
查看更多
登录 后发表回答