为什么我的页面控制器不能无限向后滚动?

Why can't my page controller scroll infinitely backwards?

我创建了一个页面控制器,它具有三个可以无限向前滚动的视图控制器(例如:1 -> 2 -> 3 -> 1 -> 等)但是当我在第一个视图控制器上并尝试向后滚动会进入白屏并且无法正确向后滚动?

我可以很好地向后滚动,直到到达第一个视图控制器,但之后它只是白屏 (例如:3 -> 2 -> 1 -> 白屏)

我希望能够无限向后滚动,所以它看起来像这样 (例如:3 -> 2 -> 1 -> 3 -> 等)

这是我的页面视图控制器代码,我不知道我做错了什么?

有人可以帮我让我的页面视图控制器无限向后滚动吗?谢谢!

    #import "PageViewController.h"

@interface PageViewController ()

@end

@implementation PageViewController
{
    NSArray *myViewControllers;


}




- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    self.dataSource = self;

    UIViewController *p1 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"ContentViewController1"];
    UIViewController *p2 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"ContentViewController2"];
    UIViewController *p3 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"ContentViewController3"];

    myViewControllers = @[p1,p2,p3];

    [self setViewControllers:@[p1]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO completion:nil];




    NSLog(@"loaded!");

}




-(UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
    return myViewControllers[index];
}



-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
     viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];

    --currentIndex;
    currentIndex = currentIndex % (myViewControllers.count);
    return [myViewControllers objectAtIndex:currentIndex];
}



-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];

    ++currentIndex;
    currentIndex = currentIndex % (myViewControllers.count);
    return [myViewControllers objectAtIndex:currentIndex];
}


-(NSInteger)presentationCountForPageViewController:
(UIPageViewController *)pageViewController
{
    return myViewControllers.count;
}

-(NSInteger)presentationIndexForPageViewController:
(UIPageViewController *)pageViewController
{
    return 0;
}


@end

在您的 viewControllerBeforeViewController 函数中,模运算符 % 没有执行您想要的操作。你的数字是这样的,% 不会让你回到第三个视图:

2、1、0、-1。

向后时,您必须检查是否已换行,然后明确转到最后一个。

感谢 Almo,我已经弄明白并重写了 viewControllerBeforeViewController 的代码。

首先我设置了currentIndex,然后我检查它显示的页面是第一个。

如果是,那么我将当前页设置为最后一页并 return 它,

如果我 运行 我的旧方法倒退,那么 return 照常进行。

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
     viewControllerBeforeViewController:(UIViewController *)viewController
{
   NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];

    if (currentIndex == 0) {
        currentIndex = 2;
    }else{
        --currentIndex;
        currentIndex = currentIndex % (myViewControllers.count);
    }

    return [myViewControllers objectAtIndex:currentIndex];
}