2
votes

I've implemented a UIPageViewController in my iPad app. However, when the iPad is in portrait you can see all the pages, but when the iPad is in landscape you can’t see the last one, and if you are in the last page in portrait and change to landscape the app crashes with the following error:

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

Because it needs 2 pages and there is only one.

What can I do when the “book” has an odd number of pages (7, for example) to avoid the previous exception?

2
My issue is similar, i am having two view controllers at a time like a book. After a couple of such views, i need to display single views on landscape itself without changing orientation. can you guide me for that. - iPhone Programmatically

2 Answers

4
votes

The way I fixed it is like this.

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

I basically check if I'm at the end of the array, or beyond it since I'm adding another page to the UIPageViewController outside of my Model array. If I'm there, and the current index is even and we're not in portrait orientation, then I add the page, if not, then I return nil.

I hope that helps.

0
votes

Take a look at my answer here. Basically in your case you have to set the UIPageViewControllerDataSource which provides the content for all pages that are not shown at the time when the view appears for the first time.