如何调试"无法识别的选择器发送到实例"问题?

Cod*_*eef 6 iphone

我在视图控制器中有以下代码(在所有其他方面)似乎工作正常:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ProblemViewController *problemViewController = [[ProblemViewController alloc] initWithNibName:@"ProblemViewController" bundle:nil];
    problemViewController.problem = (Problem*)[self.problems objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:problemViewController];
    [problemViewController release];
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此函数时,我收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[ProblemViewController initWithNibName:bundle:]: unrecognized selector sent to instance 0x57ca80'
Run Code Online (Sandbox Code Playgroud)

我对可能导致它的原因感到茫然,所以我的问题是:我应该如何最好地调试这个问题?有什么明显的我应该检查吗?

Aug*_*ust 6

检查ProblemViewController的基类.我打赌你不是从包含该initWithNibName:bundle:方法的UIViewController继承而来的.


Kev*_*ell 5

这个问题有很多答案,所有人基本上都说同样的,正确的事情:不知何故,你试图调用的方法实际上并不存在.

但是,如果你的头撞在墙上(就像我今天一小时那样),请确保首先尝试使用xcode清理项目.有时,我不知道为什么,xcode可能会进入一个糟糕的状态,并且无法正确地将您的项目编译到模拟器中.它会告诉您构建成功,但是当部署到模拟器时,您会开始看到运行时错误,好像只有一半的更改被选中.所以是的,那就发生了.