调用两次之前和之后的PageViewController并且索引不计数为true

时间:2017-03-24 10:38:49

标签: ios objective-c xcode uipageviewcontroller

- (void)viewDidLoad {
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:CGRectMake(0, 0, self.viewParent.frame.size.width, self.viewParent.frame.size.height)];
initialViewController = [self viewControllerAtIndex:[[NSUserDefaults standardUserDefaults] integerForKey:@"indexAllQuestion"]];
viewControllers = [NSArray arrayWithObject:initialViewController];

[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self  addChildViewController:self.pageController];
[[self viewParent] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
self.pageController.delegate = self;
self.pageController.dataSource = self;
}


- (AllComponentQuestionViewController *)viewControllerAtIndex:(NSUInteger)index {
current_question = self.all_question_keys[index];
AllComponentQuestionViewController *childViewController = [[AllComponentQuestionViewController alloc] initCurrent_question:current_question initCurrent_worksheet:current_worksheet iniTotalQuestion:maxDataCount];

[childSwipeVC replaceObjectAtIndex:index withObject:childViewController];
childViewController.indexChild = index;
return childViewController;}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [(AllComponentQuestionViewController *)viewController indexChild];
if ((index == NSNotFound) || index <= 0) {
    index = 0;
    return nil;
}
else{
    index--;
    return [self viewControllerAtIndex:index];
}
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [(AllComponentQuestionViewController *)viewController indexChild];
if (index >= maxDataCount) {
    index = maxDataCount;
    return nil;
}
else{
    index++;
    return [self viewControllerAtIndex:index];
}}

http://pasted.co/de8d5246此链接与

相同

案例是:

  1. 从索引0向右滑动到索引1
  2. 索引增加索引为2,所以我的索引+2
  3. 如果我再次滑动它会+1
  4. 并告诉我现在是否在索引8中,我向左滑动
  5. 索引将减少到-6,所以我的索引-2
  6. 如果我再次向左滑动,它将索引 - 或索引=索引 - 2
  7. 提前需要帮助,一周前卡在这里 谢谢

1 个答案:

答案 0 :(得分:1)

viewControllerAfterViewController:viewControllerBeforeViewController:UIPageViewControllerDataSource方法,由UIPageViewController的内部控制器调用

这些方法用1次滑动调用两次的原因是,例如,如果向右滑动(或向前),它将为您预先准备下一个视图控制器和另一个,因此,如果您需要具有用户当前所在的确切索引,则您可能需要使用委托方法来获取当前索引,例如:

目标-C:

- (void)pageViewController:(UIPageViewController *)pageViewController 
willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers;
斯威夫特4:

func pageViewController(_ pageViewController: UIPageViewController,
                        willTransitionTo pendingViewControllers: [UIViewController])

目标-C:

- (void)pageViewController:(UIPageViewController *)pageViewController 
        didFinishAnimating:(BOOL)finished 
   previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers 
       transitionCompleted:(BOOL)completed;
斯威夫特4:

func pageViewController(_ pageViewController: UIPageViewController,
                        didFinishAnimating finished: Bool,
                        previousViewControllers: [UIViewController],
                        transitionCompleted completed: Bool)

例如,用户可以向右滑动,但转换不会通过,并且pageViewController滑动将自动返回到上一个索引,因此您需要使用完成的参数来查看索引是否已更改等。

在此处阅读更多内容:https://developer.apple.com/reference/uikit/uipageviewcontrollerdelegate/1614090-pageviewcontroller?language=objc