Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set first page in UIPageViewController

When the user is on the first page of a UIPageViewController and tries to go back, I simply return nil. In iOS 5 this works fine. It is causing a crash in iOS 6.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The number of view controllers provided (0) doesn't match the number required (1) for the requested transition'

Here is my code. When returning contentViewController instead of nil, it works fine. What should I put instead of nil?

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
  viewControllerBeforeViewController:(UIViewController *)viewController {

    contentViewController = [[ContentViewController_iPhone alloc] init];

    int currentIndex = [self.modelArray indexOfObject:[(ContentViewController_iPhone *)viewController labelContents]];

    if ((currentIndex == 0) || (currentIndex == NSNotFound)) {

        //if on the first page, can't go back
        return nil;

     }

    contentViewController.labelContents = [self.modelArray objectAtIndex:currentIndex - 1];

    return contentViewController;

}
like image 579
Jack Humphries Avatar asked Oct 08 '12 17:10

Jack Humphries


1 Answers

I had the same problem and found out it only happens if I change the delegates of the pageviewcontrollers gestureRecognizers to my own controller (this is done often if you want to cancel paging when a user taps certain areas on the page, e.g. buttons).

For me it worked to not do this reassignment for iOS 6. This is fine, as iOS handles touches a little bit different. Buttons or your own gesture recognizers on the page will work fine under iOS 6 as they have priority and will cancel paging automatically.

I hope this helps.

like image 136
Frank Hartmann Avatar answered Sep 22 '22 20:09

Frank Hartmann