UIPageViewController - how Can I jump to a particular page number?

不羁岁月 提交于 2019-11-29 01:32:56

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).

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;
}

Swift Code:

func goToPage(index: Int) {
    if index < viewControllers.count {
        pageVC!.setViewControllers([viewControllers[index]], direction: .Forward, animated: true, completion: nil)
    }
}

I use this function (I'm always in landscape, 2 page mode)

-(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;

}

@milijan answer worked great. Here is the swift version of his answer:

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!