UIPageViewController pageIndex 没有正确减少

UIPageViewController pageIndex not decreasing correctly

我通过以下方法在我的应用程序中使用 UIPageViewController:

-(ChildViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.pageImages count] == 0) || (index >= [self.pageImages count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    ChildViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];

    pageContentViewController.imageFile = self.pageImages[index];
    pageContentViewController.bodyText = self.pageBody[index];
    pageContentViewController.pageIndex = index;


    NSLog(@"Index: %i", index);

    return pageContentViewController;

}


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = ((ChildViewController*) viewController).pageIndex;

    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;

    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((ChildViewController*) viewController).pageIndex;

    if (index == NSNotFound || (index == [self.pageImages count])) {
        return nil;
    }

    index++;

    return [self viewControllerAtIndex:index];
}

我遇到的问题是,即使 pageViewController 工作正常,当我尝试 NSLog 页面索引时,我得到了一些奇怪的值。 当我前进时,索引正确递增,但当我尝试返回页面时,索引跳转而不是按预期递减。

控制台读数如下:

2015-01-02 16:11:13.808 iPregrancy+[12495:1837301] Index: 0
2015-01-02 16:11:15.305 iPregrancy+[12495:1837301] Index: 1
2015-01-02 16:11:15.677 iPregrancy+[12495:1837301] Index: 2
2015-01-02 16:11:17.071 iPregrancy+[12495:1837301] Index: 3
2015-01-02 16:11:18.356 iPregrancy+[12495:1837301] Index: 4
2015-01-02 16:11:20.006 iPregrancy+[12495:1837301] Index: 5
2015-01-02 16:11:21.806 iPregrancy+[12495:1837301] Index: 2
2015-01-02 16:11:32.075 iPregrancy+[12495:1837301] Index: 5
2015-01-02 16:11:33.458 iPregrancy+[12495:1837301] Index: 2
2015-01-02 16:11:35.708 iPregrancy+[12495:1837301] Index: 5
2015-01-02 16:11:36.943 iPregrancy+[12495:1837301] Index: 2

任何ideas/suggestions?

您是否在使用过渡样式 UIPageViewControllerTransitionStyleScroll?如果是这样,我认为该行为是由于 UIPageViewController 缓存了视图控制器。

假设您在索引为 3 的页面上,然后向左滑动。 PVC 将请求(通过 viewControllerAfterViewController)索引 3 之后的 VC。您的代码 return 是索引 4 处的 VC。但是页面视图控制器还在索引 4 之后请求 VC(甚至在您开始滑动之前)。所以你 return 索引 5 - 但 PVC 不会显示它,除非你再次向左滑动。

如果您向右滑动(同时它仍显示索引 4),它仍然引用索引 4(即索引 3)之前的 VC,因此它会显示该索引。但是,它再次请求索引 3 之前的 VC - 因此您 return 索引 2 处的页面(但除非您第二次向右滑动,否则不会显示)。

结果是,如果您在索引 3 和索引 4 之间来回交换,根据您的日志,您将看到被请求的 VC 是索引 2 和索引 5。

我假设页面视图控制器这样做 "pre-emptive loading" 是为了避免在用户继续向同一方向快速滑动时出现延迟。但是,总而言之,我认为您所观察到的情况无需担心。

编辑

如果要跟踪当前显示的索引,请使用 willTransitionToViewControllers:didFinishAnimating: 委托方法。类似于:

-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
    if ([pendingViewControllers[0] isKindOfClass:[ChildViewController class]]) {
        ChildViewController *nextVC = (ChildViewController *)pendingViewControllers[0];
        self.nextIndex = nextVC.pageIndex;
    }
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (completed) {
        // transition to next VC completed successfully
        self.currentIndex = self.nextIndex;
    } else {
        // transition was aborted
        self.nextIndex = self.currentIndex;
    }
}

您需要为 currentIndexnextIndex 添加属性,还需要设置页面视图控制器的 delegate(与您当前设置 dataSource).