Ios ';无法识别的选择器发送到实例';将UITableView添加到UIPageViewController时
我正在尝试创建一个包含多个水平分页UITableView的PageViewController。UITableView存储在一个数组中;我使用UIPageViewControllerIos ';无法识别的选择器发送到实例';将UITableView添加到UIPageViewController时,ios,objective-c,uitableview,uipageviewcontroller,Ios,Objective C,Uitableview,Uipageviewcontroller,我正在尝试创建一个包含多个水平分页UITableView的PageViewController。UITableView存储在一个数组中;我使用UIPageViewControllerSetViewController方法将数组中的第一个对象添加到PageVC: [self.pageViewController setViewControllers:[self.tableViews objectAtIndex:0] direction:UIPageViewControllerNavigation
SetViewController
方法将数组中的第一个对象添加到PageVC:
[self.pageViewController setViewControllers:[self.tableViews objectAtIndex:0] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
然后,我使用viewControllerBeforeViewController
和viewControllerAfterViewController
方法将剩余的uitableview添加到PageViewController
但是,
setViewControllers
方法产生以下错误:
-[UITableView count]: unrecognized selector sent to instance 0x7c347e00
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView count]: unrecognized selector sent to instance 0x7c347e00'
为什么会发生此错误?有两个问题
您需要将视图控制器而不是视图传递给页面视图控制器。对于每个UITableView,都需要一个UITableViewController
如果将变量self.tableViews
更改为self.tableViewControllers
(使用为每个UITableView创建的UITableViewController),您已经成功了一半
另一个问题是
setViewControllers
的第一个参数应该是NSArray。(NSArray有一个名为count
的选择器)
更改代码,以便在添加第一个表视图时将其包装到数组中:
[self.pageViewController setViewControllers:@[ [self.tableViewControllers objectAtIndex:0] ] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]
两个问题
您需要将视图控制器而不是视图传递给页面视图控制器。对于每个UITableView,都需要一个UITableViewController
如果将变量self.tableViews
更改为self.tableViewControllers
(使用为每个UITableView创建的UITableViewController),您已经成功了一半
另一个问题是
setViewControllers
的第一个参数应该是NSArray。(NSArray有一个名为count
的选择器)
更改代码,以便在添加第一个表视图时将其包装到数组中:
[self.pageViewController setViewControllers:@[ [self.tableViewControllers objectAtIndex:0] ] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]
您应该先看看您使用的方法:
-(void)SetViewController:(NSArray*)ViewController
方向:(UIPageViewControllerNavigationDirection)方向
动画:(BOOL)动画
完成:(作废(^)(BOOL已完成))完成;
它说参数应该是视图控制器
参数:
viewControllers要创建的一个或多个视图控制器
显示
因此,您必须使用
UIViewcontroller
或UITableViewController
您应该先看看您使用的方法:
-(void)SetViewController:(NSArray*)ViewController
方向:(UIPageViewControllerNavigationDirection)方向
动画:(BOOL)动画
完成:(作废(^)(BOOL已完成))完成;
它说参数应该是视图控制器
参数:
viewControllers要创建的一个或多个视图控制器
显示
因此,您必须使用
UIViewcontroller
或UITableViewController
显示用于创建TableView数组的代码,确保所有这些都正确创建并设置了数据源在pageViewController
@user2181948中使用tableview
作为viewcontroller
看起来有点奇怪,它是否在viewControllers参数中工作?因为您使用了UITableView
而不是UIViewController
作为参数。@AnuragSharma现在由于未捕获的异常“NSInvalidArgumentException”,我收到了终止应用程序的错误,原因:'-[UITableView parentViewController]:无法识别的选择器发送到实例
,这是一个完全不同的错误。您应该使用viewController
作为参数!看看我的答案和Apple API文档。显示用于创建TableView数组的代码,确保所有这些都正确创建并设置了数据源在pageViewController
@user2181948中使用tableview
作为viewcontroller
看起来有点奇怪,它是否在viewControllers参数中工作?因为您使用了UITableView
而不是UIViewController
作为参数。@AnuragSharma现在由于未捕获的异常“NSInvalidArgumentException”,我收到了终止应用程序的错误,原因:'-[UITableView parentViewController]:无法识别的选择器发送到实例
,这是一个完全不同的错误。您应该使用viewController
作为参数!看看我的答案和苹果API文档。setViewControllers
不能是NSObject
UITableView
是NSObject
的子类UIViewController
应该在参数中传递到这里。我没有注意到原来的问题有UITableView
s而不是UITableViewController
s。我已经修复了.OP已经提到的它是UITableView
s。我的观点是,在发布答案之前,你应该自己检查两三次。setViewControllers
不能是NSObject
UITableView
是NSObject
的子类UIViewController
应该在参数中传递到这里。我没有注意到原来的问题有UITableView
s而不是UITableViewController
s。我已经修复了.OP已经提到的它是UITableView
s。我的观点是,在发布答案之前,你应该自己检查2、3次。