简体   繁体   中英

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;

}

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, eg 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.

You need to add the following to your code

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

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

 }

Latest version of Xcode Page Based Application template provides the following code:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) {
    return nil;
}

index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}

That should do the trick :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM