如何滚动到UIPageViewController上的某个页面?
我有一个完美的UIPageViewController但是我有一个UICollectionView,它包含UIPageviewController的所有页面。我想要做的是当用户点击某个页面时,我导航回我的UIPageViewController并显示被点击的页面。
我就这样做了:
获取tapped page = page number
执行segue并将页码发送到UIPageViewController,
进入我的reloadPageviewController
方法,它抓取viewControllers
数组中的视图控制器并使用setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil
设置视图控制器,然后添加子控制器(实际内容视图)控制器)并添加子视图。
我记录了视图控制器的索引,它记录了所需的索引(被点击的页面)但在屏幕上显示的是第一页。
这是我的代码:
从CollectionView查询到UIPageViewController
if ([segue.identifier isEqualToString:@"specificLocation"]) {
//index here is good
NSLog(@"index for vc clicked:%d", chosenIndexPath.item);
[segue.destinationViewController performSelectorOnMainThread:@selector(prepareData:) withObject:[NSNumber numberWithInteger:chosenIndexPath.item] waitUntilDone:YES];
}
UIPageViewController
-(void)prepareData:(NSNumber *) index{
NSInteger index2 = [index integerValue];
//Correct index received here
NSLog(@"Receieved index:%d", index2);
[self reloadPageViewControllerWithIndex:index2];
}
-(void)reloadPageViewControllerWithIndex:(NSInteger)index{
//SET UP THE first PAGE
j=0;
self.viewcontrollers = [[NSMutableArray alloc]init];
for (int i=0;i<20;i++) {
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
vc.index = i;
vc.title =[NSString stringWithFormat:@"Page :%d", i];
[self.viewcontrollers addObject:vc];
}
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
NSLog(@"Index for requested vc:%d and total number of vcs:%d",index, [self.viewcontrollers count]);
ViewController *vc2 = [self viewControllerForIndex:index];
NSArray *viewControllers = @[vc2];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
所以我的问题是即使我有想要显示的视图控制器的正确索引,我的UIPageViewController总是会在self.viewcontrollers数组中显示第一个视图控制器