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

2020年11月24日 81点热度 0条评论 来源: Cool Kat Studios

我有一个方法在我的页面视图控制器类,这意味着产生一组数字一次,但相反,似乎因为某种原因两次激发?谁能告诉我为什么它的两次发射?为什么我的方法调用两次?

这是控制台火灾

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


===========解决方案如下:

我认为你UIPageViewController需要3控制器(一般),以便能够正确显示其内容:

  1. 主要可见控制器
  2. 上一个
  3. 下一个

此以前和下一个是部分可见的部分时,您的滚动控制器。

因此,您的2种方法不是针对滑动方向分别调用的,而是在您每次滚动控制器时调用。因此,您的滑动内容会被评估两次。

    原文作者:Cool Kat Studios
    原文地址: https://stackoverflow.com/q/30066078
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。