iPhone: “unrecognized selector sent to instance”

2019-02-27 22:42发布

I try to set two different views into a subview, according to the state of a SegmentSwitcher:

if ([sender selectedSegmentIndex] == gameIndex) {
    if (self.gameView.view == nil) {
        GameView *gameV = [[UIViewController alloc] initWithNibName:@"GameView" bundle:nil];
        self.gameView = gameV;
        [gameV release];
    }
    [tableView.view removeFromSuperview];
    [subView insertSubview:gameView.view atIndex:0];
} else {
    if (self.tableView.view == nil) {
        TableView *tableV = [[UIViewController alloc] initWithNibName:@"TableView" bundle:nil];
        self.tableView = tableV;
        [tableV release];
    }
    [tableView.view removeFromSuperview];
    [subView insertSubview:tableView.view atIndex:0];
}

TableView extends TableViewController, but I always get the following error when I try to switch to the tableview:

2010-01-06 19:55:00.871 Handball[84675:40b] * -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x3b18360 2010-01-06 19:55:00.873 Handball[84675:40b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x3b18360' 2010-01-06 19:55:00.874 Handball[84675:40b] Stack: (

Any help would be REALLY, REALLY appreciated...

1条回答
Juvenile、少年°
2楼-- · 2019-02-27 23:17

While tableV is declared to be a TableView, it's most likely initialized with a simple UIViewConrtoller, as it appears in your code. Try changing the line to:

TableView *tableV = [[TableView alloc] initWithNibName:@"TableView" bundle:nil];

And TableView should be a subtype of UITableViewController.

By the way, the same should probably happen with GameView as well.

查看更多
登录 后发表回答