UIPageViewController – 如何跳转到特定的页码?
我已经花了近8小时了解如何跳转到UIPageViewController中的特定页码…下面是我的项目看起来像
我想做一个应用程序,看起来像Ibooks —
我已经从这里提供的代码的帮助 – http://www.ioslearner.com/tag/uipageviewcontroller-sample-code/
我做了一个plist和UITableView,我从TableView中select的值,并显示在UIPAgeiewController上放置的webView相同,但问题是,只有在Web视图中的页面更改,而不是UIPageViewController的实际页码….
如果我缩小我的问题,它会看起来像是—有没有办法在UIPageViewController中的页码之间切换…. ???
你必须实例化你想要跳转的页面的视图控制器,然后调用页面视图控制器的setViewControllers:direction:animated:completion:
方法,传递新的视图控制器。
这里是另一个例子如何跳转到parsing的页面索引:
-(void)gotoPage:(int)index{ SinglePageViewController *viewController = [self viewControllerAtIndex:index]; UIPageViewControllerNavigationDirection direction; if(_curIndex <= index){ direction = UIPageViewControllerNavigationDirectionForward; } else { direction = UIPageViewControllerNavigationDirectionReverse; } if(_curIndex < index) { for (int i = 0; i <= index; i++) { if (i == index) { [self.pageViewController setViewControllers:@[viewController] direction:direction animated:YES completion:nil]; } else { [self.pageViewController setViewControllers:@[[self viewControllerAtIndex:i]] direction:direction animated:NO completion:nil]; } } } else { for (int i = _curIndex; i >= index; i--) { if (i == index) { [self.pageViewController setViewControllers:@[viewController] direction:direction animated:YES completion:nil]; } else { [self.pageViewController setViewControllers:@[[self viewControllerAtIndex:i]] direction:direction animated:NO completion:nil]; } } } _curIndex = index; }
我使用这个function(我总是在风景,2页模式)
-(void) flipToPage:(NSString * )index { int x = [index intValue]; LeafletPageContentViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; NSUInteger retreivedIndex = [self indexOfViewController:theCurrentViewController]; LeafletPageContentViewController *firstViewController = [self viewControllerAtIndex:x]; LeafletPageContentViewController *secondViewController = [self viewControllerAtIndex:x+1 ]; NSArray *viewControllers = nil; viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, nil]; if (retreivedIndex < x){ [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; } else { if (retreivedIndex > x ){ [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL]; } } }
也许你也需要这个
- (LeafletPageContentViewController *)viewControllerAtIndex:(NSUInteger)index { if (([self.modelArray count] == 0) || (index >= [self.modelArray count])) { return nil; } LeafletPageContentViewController *dataViewController; dataViewController = [[LeafletPageContentViewController alloc]initWithNibName:@"LeafletPageContentViewController" bundle:nil]; dataViewController.dataObject = [self.modelArray objectAtIndex:index]; return dataViewController; }
@milijan回答很好。 这是他的答案的迅速版本:
func jumptoPage(index : Int) { let vc = viewControllerAtIndex(index) let direction : UIPageViewControllerNavigationDirection! if currentIndex < index { direction = UIPageViewControllerNavigationDirection.Forward } else { direction = UIPageViewControllerNavigationDirection.Reverse } if (currentIndex < index) { for var i = 0; i <= index; i++ { if (i == index) { self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil) } else { self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil) } } } else { for var i = currentIndex; i >= index; i = i - 1 { if i == index { self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil) } else { self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil) } } } currentIndex = index }
SWIFT代码:
func goToPage(index: Int) { if index < viewControllers.count { pageVC!.setViewControllers([viewControllers[index]], direction: .Forward, animated: true, completion: nil) } }