在UIPageViewController中手动切换视图会导致异常

时间:2014-03-29 22:02:46

标签: ios objective-c

我有一个UIPageViewController,它显示两个UIViewControllers并允许用户在两​​者之间滑动。我实现了一个按钮,允许用户在两​​者之间手动切换。执行此操作的代码如下:

[self setViewControllers:@[[self.storyboard instantiateViewControllerWithIdentifier:@"PlayersViewController"]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL completion){}];

不幸的是,每次按下按钮,我都会收到一个例外情况:

  

*由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' * - [__ NSPlaceholderArray initWithObjects:count:]:尝试从对象插入nil对象[ 0]'

我从我的自定义类中调用该方法,该类扩展了UIPageViewControllerDelegate。

非常感谢任何帮助!

**编辑:

以下是我的自定义类的相关部分:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataSource = self;
    gvc = [[GamesViewController alloc] init];
    pvc = [[PlayersViewController alloc] init];
    [self setViewControllers:@[[self.storyboard instantiateViewControllerWithIdentifier:@"GamesViewController"]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

- (void) slidepage
{
    [self setViewControllers:@[[self.storyboard instantiateViewControllerWithIdentifier:@"PlayersViewController"]] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:^(BOOL completion){NSLog(@"hihi");}];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[GamesViewController class]])
        return nil;

    return [self.storyboard instantiateViewControllerWithIdentifier:@"GamesViewController"];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[PlayersViewController class]])
        return nil;

    return [self.storyboard instantiateViewControllerWithIdentifier:@"PlayersViewController"];
}

1 个答案:

答案 0 :(得分:2)

似乎

[self.storyboard instantiateViewControllerWithIdentifier:@"PlayersViewController"]

返回nil。这意味着self.storyboardnil或故事板无法找到给定标识符的视图控制器。