- (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];
}}
相同
案例是:
提前需要帮助,一周前卡在这里 谢谢
答案 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滑动将自动返回到上一个索引,因此您需要使用完成的参数来查看索引是否已更改等。