PageViewController错误地显示页面的索引?

时间:2016-06-14 05:28:16

标签: ios objective-c uipageviewcontroller

我已经按照本教程创建了PageViewController http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/ 问题是如何让页面错误地完成其余的事情都是完美的

我需要做一些事情,比如如果页面索引是最后一个索引,那么我已经更改了按钮标签。

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

    // Create a new view controller and pass suitable data.
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
    pageContentViewController.imageFile = self.pageImages[index];
    pageContentViewController.titleText = self.pageTitles[index];

    pageContentViewController.pageIndex = index;

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

    return pageContentViewController;
}

- (void)pageViewController:(UIPageViewController *)pageViewCntr didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    NSInteger currentIndex = ((UIViewController *)self.pageViewController.viewControllers.firstObject).view.tag;
    if (completed) pageControl.currentPage = currentIndex;
}

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

    if (index == NSNotFound || !index) return nil;
    NSLog(@"INDEX : %lu", (unsigned long)index);
    return [self viewControllerAtIndex:index-1];
}

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

    if (index == NSNotFound || index == pageCount-1) return nil;
    NSLog(@"INDEX : %lu", (unsigned long)index);
    return [self viewControllerAtIndex:index+1];
}



- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{

    return [self.pageImages count];

}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{

    return 0;
}

我尝试了很多方法,但无法解决问题。

帮我查找页面的当前索引。 目前我没有显示页面的索引,当我轻扫一次时,它一次返回0,1索引并且对于下一次刷卡它显示我2并且对于下一次刷卡它不会返回任何索引

1 个答案:

答案 0 :(得分:1)

我已经实现了相同的代码,经过调试后,我们知道PageViewControllerDataSource方法被调用了两次。因此,您可以使用willTransitionToViewControllers方法代替viewControllerBeforeViewControllerviewControllerAfterViewController方法来获取正确的索引。

请参阅以下答案,

PageViewController delegate functions called twice