UIPageViewController pageIndex没有正确减少

时间:2015-01-02 14:29:40

标签: ios uipageviewcontroller

我在我的应用程序中使用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

有任何想法/建议吗?

1 个答案:

答案 0 :(得分:3)

您使用转换样式UIPageViewControllerTransitionStyleScroll吗?如果是这样,我认为该行为是由于UIPageViewController缓存视图控制器。

假设您在索引为3的页面上,并向左滑动。 PVC将在索引3之后请求(通过viewControllerAfterViewController)VC。您的代码在索引4处返回VC。但页面视图控制器也在索引4之后请求VC(甚至在您开始滑动之前)。因此,您返回索引5 - 但除非您再次向左滑动,否则PVC不会显示。

如果您向右滑动(当它仍然显示索引4时),它仍然具有对索引4之前的VC的引用(即索引3),因此它显示了该值。但是,同样,它还在索引3之前请求VC - 所以你在索引2处返回页面(但除非你再次向右滑动,否则不会显示)。

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

我假设页面视图控制器执行此“抢先加载”,以避免在用户继续沿同一方向快速滑动时出现延迟。但是,总而言之,我认为您所观察到的内容无需担心。

修改

如果要跟踪当前显示的索引,请使用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(与您当前设置{{1}的位置相同}})。