为什么我的方法调用两次?

时间:2015-05-06 01:44:54

标签: ios objective-c uipageviewcontroller

我的页面视图控制器类中有一个方法,它意味着产生一组数字,但是由于某些原因它似乎会发射两次?谁能告诉我为什么它会两次射击?

这是控制台触发的内容

s18.postimg.org/f4de63swp/Screen_Shot_2015_05_06_at_1_42_03_pm.png

发生了什么,为什么它不应该像它一样工作?

我希望我的页面视图控制器在用户向前滚动时循环,而我有五个不同的数字,它们不断同时上升但不循环,它们只是无限增加。

向后滑动方法

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


    swipeTotal = swipeTotal - 1;



    if (swipeTotal > 4) {
        firstFive = true;
        //   NSLog(@"First Five Passed");
    }else{
        firstFive = false;
        //  NSLog(@"First Five False");
    }

    // NSLog(@"Swipe Total is %d",swipeTotal);

    if (currentIndex == 0) {
        swipeContent1 = swipeTotal;
        swipeContent2 = swipeTotal + 1;
        swipeContent3 = swipeTotal + 2;
        swipeContent4 = swipeTotal - 2;
        swipeContent5 = swipeTotal - 1;
    }
    if (currentIndex == 1) {
        swipeContent1 = swipeTotal - 1;
        swipeContent2 = swipeTotal;
        swipeContent3 = swipeTotal + 1;
        swipeContent4 = swipeTotal + 2;
        swipeContent5 = swipeTotal - 2;
    }
    if (currentIndex == 2) {
        swipeContent1 = swipeTotal - 2;
        swipeContent2 = swipeTotal - 1;
        swipeContent3 = swipeTotal;
        swipeContent4 = swipeTotal + 1;
        swipeContent5 = swipeTotal + 2;
    }
    if (currentIndex == 3) {
        swipeContent1 = swipeTotal + 2;
        swipeContent2 = swipeTotal - 2;
        swipeContent3 = swipeTotal - 1;
        swipeContent4 = swipeTotal;
        swipeContent5 = swipeTotal + 1;
    }
    if (currentIndex == 4) {
        swipeContent1 = swipeTotal + 1;
        swipeContent2 = swipeTotal + 2;
        swipeContent3 = swipeTotal - 2;
        swipeContent4 = swipeTotal - 1;
        swipeContent5 = swipeTotal;
    }

    if (firstFive == true) {


    if (currentIndex == 0) {
        currentIndex = 4;

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

    }else{
        if (currentIndex > 0)
        {
            return [myViewControllers objectAtIndex:currentIndex-1];
            // return the previous viewcontroller
        } else
        {
            return nil;
            // do nothing
        }

    }


     /*
        content1 = [users objectAtIndex:swipeContent1];
        content2 = [users objectAtIndex:swipeContent2];
        content3 = [users objectAtIndex:swipeContent3];
        content4 = [users objectAtIndex:swipeContent4];
        content5 = [users objectAtIndex:swipeContent5]; */

    NSLog(@"Content 1 = %d",swipeContent1);
    NSLog(@"Content 2 = %d",swipeContent2);
    NSLog(@"Content 3 = %d",swipeContent3);
    NSLog(@"Content 4 = %d",swipeContent4);
    NSLog(@"Content 5 = %d",swipeContent5);




    return [myViewControllers objectAtIndex:currentIndex];
}

向前滑动方法

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


    if (swipeTotal > 4) {
        firstFive = true;

        //      NSLog(@"First Five Passed");
    }else{
        firstFive = false;
        //     NSLog(@"First Five False");
    }

    //  NSLog(@"Swipe Total is %d",swipeTotal);

    ++currentIndex;
    currentIndex = currentIndex % (myViewControllers.count);

    swipeTotal = swipeTotal + 1;

    if (currentIndex == 0) {
        swipeContent1 = swipeTotal;
        swipeContent2 = swipeTotal + 1;
        swipeContent3 = swipeTotal + 2;
        swipeContent4 = swipeTotal - 2;
        swipeContent5 = swipeTotal - 1;
    } else if (currentIndex == 1) {
        swipeContent1 = swipeTotal - 1;
        swipeContent2 = swipeTotal;
        swipeContent3 = swipeTotal + 1;
        swipeContent4 = swipeTotal + 2;
        swipeContent5 = swipeTotal - 2;
    } else if (currentIndex == 2) {
        swipeContent1 = swipeTotal - 2;
        swipeContent2 = swipeTotal - 1;
        swipeContent3 = swipeTotal;
        swipeContent4 = swipeTotal + 1;
        swipeContent5 = swipeTotal + 2;
    } else if (currentIndex == 3) {
        swipeContent1 = swipeTotal + 2;
        swipeContent2 = swipeTotal - 2;
        swipeContent3 = swipeTotal - 1;
        swipeContent4 = swipeTotal;
        swipeContent5 = swipeTotal + 1;
    } else if (currentIndex == 4) {
        swipeContent1 = swipeTotal + 1;
        swipeContent2 = swipeTotal + 2;
        swipeContent3 = swipeTotal - 2;
        swipeContent4 = swipeTotal - 1;
        swipeContent5 = swipeTotal;
    }

    NSLog(@"Content 1 = %d",swipeContent1);
    NSLog(@"Content 2 = %d",swipeContent2);
    NSLog(@"Content 3 = %d",swipeContent3);
    NSLog(@"Content 4 = %d",swipeContent4);
    NSLog(@"Content 5 = %d",swipeContent5);


    return [myViewControllers objectAtIndex:currentIndex];
}

1 个答案:

答案 0 :(得分:0)

我认为你的UIPageViewController需要3个控制器(一般来说)才能正确显示其内容:

  1. 主要可见控制器
  2. 前一个
  3. 下一个
  4. 当您的滚动部分是您的控制器时,此上一个和下一个部分可见。

    因此,对于滑动方向,不会调用每种方法,但每次滚动控制器时都会调用这些方法。因此,您的滑动内容会被评估两次。