OGeek|极客世界-中国程序员成长平台

标题: ios - UIPageViewController 上的奇数页数 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:48
标题: ios - UIPageViewController 上的奇数页数

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

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘The >number of provided view controllers (1) doesn’t match the number required (2) for the >requested spine location (UIPageViewControllerSpineLocationMid)’

因为它需要两页,而只有一页。

当“书”的页数为奇数(例如 7 页)时,我该怎么做才能避免前面的异常?



Best Answer-推荐答案


我修的方法是这样的。

- (UIViewController *)pageViewControllerUIPageViewController *)pageViewController viewControllerAfterViewControllerUIViewController *)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;
}

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

希望对你有帮助。

关于ios - UIPageViewController 上的奇数页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10582934/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4