我通过以下方法在我的应用程序中使用 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
有什么想法/建议吗?
最佳答案
你用的是过渡风格吗UIPageViewControllerTransitionStyleScroll
?如果是这样,我认为该行为是由于 UIPageViewController 缓存了 View Controller 。
假设您在索引为 3 的页面上,然后向左滑动。 PVC 将(通过 viewControllerAfterViewController
)在索引 3 之后请求 VC。您的代码返回索引 4 处的 VC。但是页面 View Controller 也会在索引 4 之后请求 VC(甚至在您开始滑动之前)。因此,您返回索引 5 - 但除非您再次向左滑动,否则 PVC 不会显示该索引。
相反,如果您向右滑动(虽然它仍然显示索引 4),它仍然具有对索引 4 之前的 VC 的引用(即索引 3),因此它会显示它。但是,同样,它还在索引 3 之前请求 VC - 因此您返回索引 2 处的页面(但除非您第二次向右滑动,否则不会显示)。
结果是,如果您在索引 3 和索引 4 之间来回交换,您将看到被请求的 VC 是索引 2 和索引 5 - 根据您的日志。
我假设页面 View Controller 执行此“抢先加载”是为了避免用户在同一方向快速滑动时出现延迟。但是,总而言之,我不认为你所观察到的有什么可担心的。
编辑
如果要跟踪当前显示的索引,请使用 willTransitionToViewControllers:
和 didFinishAnimating:
委托(delegate)方法。就像是:
-(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;
}
}
您需要为
currentIndex
添加属性和 nextIndex
, 并设置 delegate
页面 View Controller (在与您当前设置 dataSource
相同的位置)。
关于ios - UIPageViewController pageIndex 没有正确减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743732/