如何在UIPageViewController中设置第一页

时间:2023-01-03 21:12:06

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.

当用户在UIPageViewController的第一页上并试图返回时,我只返回nil。在ios5中,这很好用。它导致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?

这是我的代码。当返回contentViewController而不是nil时,它工作得很好。我应该放什么而不是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;

}

3 个解决方案

#1


10  

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

我遇到了同样的问题,发现只有当我将pageviewcontrollergesturerecognizers的委托更改为我自己的控制器时才会发生这种情况(如果您希望在用户点击页面上的某些区域时取消分页,例如按钮)。

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.

对我来说,不做iOS 6的重新分配很管用。这很好,因为iOS处理有点不同。在ios6下,页面上的按钮或手势识别器可以正常工作,因为它们具有优先级,并且会自动取消分页。

I hope this helps.

我希望这可以帮助。

#2


0  

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

基于Xcode页面的应用程序模板的最新版本提供了以下代码:

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


0  

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;

 }

#1


10  

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

我遇到了同样的问题,发现只有当我将pageviewcontrollergesturerecognizers的委托更改为我自己的控制器时才会发生这种情况(如果您希望在用户点击页面上的某些区域时取消分页,例如按钮)。

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.

对我来说,不做iOS 6的重新分配很管用。这很好,因为iOS处理有点不同。在ios6下,页面上的按钮或手势识别器可以正常工作,因为它们具有优先级,并且会自动取消分页。

I hope this helps.

我希望这可以帮助。

#2


0  

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

基于Xcode页面的应用程序模板的最新版本提供了以下代码:

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


0  

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;

 }