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

时间:2015-04-23 21:20:08

标签: ios objective-c uipageviewcontroller infinite-scroll

我创建了一个页面控制器,它有三个可以无限向前滚动的视图控制器(例如: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

2 个答案:

答案 0 :(得分:1)

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

2,1,0,-1。

向后退时,你必须检查你是否已经包裹然后明确地转到最后一个。

答案 1 :(得分:0)

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

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

如果是,那么我将当前页面设置为最后一页并将其返回,

如果不是我运行我的旧方法倒退,那么像往常一样返回它。

-(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];
}