问题
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;
}
回答1:
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.
回答2:
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 :-)
回答3:
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;
}
来源:https://stackoverflow.com/questions/12786641/how-to-set-first-page-in-uipageviewcontroller