I have spent almost 8 hours finding out how to jump to a particular page number in UIPageViewController... below is what my project looks like
我花了将近8个小时来了解如何跳转到UIPageViewController中的特定页码...下面是我的项目看起来像
I want to make an app which looks like Ibooks---
我想制作一个看起来像Ibooks的应用---
I have taken the help from the code presented here - http://www.ioslearner.com/tag/uipageviewcontroller-sample-code/
我已经从这里提供的代码中获得了帮助 - http://www.ioslearner.com/tag/uipageviewcontroller-sample-code/
I have made a plist and UITableView , I select the value from TableView and display the same on webView placed on UIPAgeiewController, but the problem is that only the page in web view changes and not the actual page number of the UIPageViewController....
我已经制作了一个plist和UITableView,我从TableView中选择了值并在UIPAgeiewController上放置的webView上显示相同,但问题是只有web视图中的页面发生了变化,而不是UIPageViewController的实际页面编号....
If I narrow down my question it would look like --- is there a way to switch between page numbers in UIPageViewController.... ???
如果我缩小我的问题,它看起来像---是否有办法在UIPageViewController中切换页码....
5 个解决方案
#1
5
You have to instantiate the view controller(s) that manage the page(s) you want to jump to and then call the page view controller's setViewControllers:direction:animated:completion:
method, passing the new view controller(s).
您必须实例化管理要跳转到的页面的视图控制器,然后调用页面视图控制器的setViewControllers:direction:animated:completion:方法,传递新的视图控制器。
#2
3
Here is another example how to jump to page with parsed Index:
这是另一个如何使用已解析的索引跳转到页面的示例:
-(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;
}
#3
3
Swift Code:
SWIFT代码:
func goToPage(index: Int) {
if index < viewControllers.count {
pageVC!.setViewControllers([viewControllers[index]], direction: .Forward, animated: true, completion: nil)
}
}
#4
2
I use this function (I'm always in landscape, 2 page mode)
我使用这个功能(我总是在风景,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];
}
}
}
Maybe you need this too
也许你也需要这个
- (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;
}
#5
2
@milijan answer worked great. Here is the swift version of his answer:
@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
}
#1
5
You have to instantiate the view controller(s) that manage the page(s) you want to jump to and then call the page view controller's setViewControllers:direction:animated:completion:
method, passing the new view controller(s).
您必须实例化管理要跳转到的页面的视图控制器,然后调用页面视图控制器的setViewControllers:direction:animated:completion:方法,传递新的视图控制器。
#2
3
Here is another example how to jump to page with parsed Index:
这是另一个如何使用已解析的索引跳转到页面的示例:
-(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;
}
#3
3
Swift Code:
SWIFT代码:
func goToPage(index: Int) {
if index < viewControllers.count {
pageVC!.setViewControllers([viewControllers[index]], direction: .Forward, animated: true, completion: nil)
}
}
#4
2
I use this function (I'm always in landscape, 2 page mode)
我使用这个功能(我总是在风景,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];
}
}
}
Maybe you need this too
也许你也需要这个
- (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;
}
#5
2
@milijan answer worked great. Here is the swift version of his answer:
@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
}