16

我花了将近 8 个小时来了解如何跳转到 UIPageViewController 中的特定页码……下面是我的项目的样子

我想做一个看起来像 Ibooks 的应用程序---

我从这里提供的代码中获得了帮助 - http://www.ioslearner.com/tag/uipageviewcontroller-sample-code/

我制作了一个 plist 和 UITableView ,我从 TableView 中选择值并在放置在 UIPAgeiewController 上的 webView 上显示相同的值,但问题是只有 web 视图中的页面发生了变化,而不是 UIPageViewController 的实际页码....

如果我缩小我的问题范围,它看起来像---有没有办法在 UIPageViewController 中的页码之间切换......?

4

5 回答 5

6

您必须实例化管理要跳转到的页面的视图控制器,然后调用页面视图控制器的setViewControllers:direction:animated:completion:方法,传递新的视图控制器。

于 2012-07-05T12:57:03.637 回答
6

SWIFT代码:

func goToPage(index: Int) {
    if index < viewControllers.count {
        pageVC!.setViewControllers([viewControllers[index]], direction: .Forward, animated: true, completion: nil)
    }
}
于 2016-01-18T15:40:08.113 回答
3

这是另一个如何跳转到已解析索引的页面的示例:

-(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;
}
于 2015-09-15T07:31:38.540 回答
2

我使用这个功能(我总是在横向,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;

}
于 2012-08-10T09:13:53.580 回答
2

@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
}
于 2015-10-21T16:30:42.923 回答