Search code examples
iosobjective-cuitableviewuipageviewcontroller

'unrecognized selector sent to instance' when adding UITableView to UIPageViewController


I am trying to create a PageViewController with multiple horizontally-paging UITableViews inside. The UITableViews are stored in an array; I use the UIPageViewController setViewControllers method to add the very first object in the array to the PageVC:

 [self.pageViewController setViewControllers:[self.tableViews objectAtIndex:0] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

Then I add the remaining UITableViews to the PageViewController by using the viewControllerBeforeViewController and viewControllerAfterViewController methods.

However the setViewControllers method is producing the following error:

 -[UITableView count]: unrecognized selector sent to instance 0x7c347e00
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView count]: unrecognized selector sent to instance 0x7c347e00'

Why is this error happening?


Solution

  • You should have a look at the method first you are using here:

    - (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers 
                     direction:(UIPageViewControllerNavigationDirection)direction 
                      animated:(BOOL)animated 
                    completion:(void (^)(BOOL finished))completion;
    

    It says the parameters should be the viewControllers

    Parameters:

    viewControllers The view controller or view controllers to be displayed.

    So, you have to take a UIViewcontroller or UITableViewController.