Drew K.
Drew K.

Reputation: 177

Manually switching views in UIPageViewController causes exception

I have a UIPageViewController that displays two UIViewControllers and allows the user to swipe between the two. I implemented a button that allows the user to manually switch between the two. The code to execute this is as follows:

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

Unfortunately, every time I press the button, I get an exception that says:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

I call the method from my custom class that extends the UIPageViewControllerDelegate.

Any help would be much appreciated!

**Edit:

Here are relevant portions of my custom class:

- (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"];
}

Upvotes: 0

Views: 278

Answers (1)

Alfonso
Alfonso

Reputation: 8482

It seems that

[self.storyboard instantiateViewControllerWithIdentifier:@"PlayersViewController"]

returns nil. This would mean that either self.storyboard is nil or the storyboard can't find a view controller for the given identifier.

Upvotes: 2

Related Questions