UIPageViewController上的奇数页数

时间:2012-05-14 12:03:37

标签: ios ipad uipageviewcontroller

我在我的iPad应用程序中实现了一个UIPageViewController。但是,当iPad处于纵向状态时,您可以看到所有页面,但是当iPad处于横向状态时,您无法看到最后一页,如果您处于纵向的最后一页并且更改为横向,则应用程序会崩溃以下错误:

  

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'>提供的视图控制器(1)的数量与>请求的主干位置(UIPageViewControllerSpineLocationMid)所需的数量(2)不匹配

因为它需要2页而且只有一页。

当“书”有奇数页(例如7)以避免先前的例外时,我该怎么办?

2 个答案:

答案 0 :(得分:4)

我修复它的方式是这样的。

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    DSBookletPageViewController *currentVC = (DSBookletPageViewController *)viewController;
    NSUInteger currentIndex = [currentVC index];

    if(currentIndex >= self.modelArray.count-1) {
        if (currentIndex %2 == 0 && !isPortrait) {
            //return an empty one
            DSBookletPageViewController *newVC = [[DSBookletPageViewController alloc] init];

            [newVC setIndex:currentIndex+1];

            return newVC;
        } else {
            return nil;
        }
    }

    currentIndex ++;

    UIImage *currentPage = [self.modelArray objectAtIndex:currentIndex];
    DSBookletPageViewController *newVC = [[DSBookletPageViewController alloc] initWithImage:currentPage andOrientation:isPortrait];
    [newVC setIndex:currentIndex];

    return newVC;
}

我基本上检查我是否在数组的末尾,或者超出它,因为我在我的Model数组之外的UIPageViewController中添加了另一个页面。如果我在那里,并且当前索引是偶数且我们不是纵向,那么我添加页面,如果没有,那么我返回nil。

我希望有所帮助。

答案 1 :(得分:0)

看看我的回答here。基本上在您的情况下,您必须设置UIPageViewControllerDataSource,它为首次出现视图时未显示的所有页面提供内容。