在prepareForSegue中设置属性时出错:

时间:2015-06-13 16:06:06

标签: ios objective-c

尝试在准备segue的公共属性中设置字符串时,我收到以下错误。知道为什么吗?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setQuestionObjectId:]: unrecognized selector sent to instance 0x7fa713562b40'

代码是:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"postSegue"]) {
    CommentsViewControllerNew *commentsVC = (CommentsViewControllerNew *)[segue destinationViewController];
    commentsVC.hidesBottomBarWhenPushed = YES;
    PFObject * question=[self.brightenArray objectAtIndex:self.indexPathOfClickedpost.row];
    commentsVC.questionObjectId=question.objectId;
    NSLog(@"%@",[self.Array objectAtIndex:self.indexPathOfClickedpost.row]);
//        commentsVC.question = question;

1 个答案:

答案 0 :(得分:0)

正如您可能已经收集的那样,这是因为您正在向不识别该消息的类发送setQuestionObjectId:消息。例如。如果您将reloadData(来自UITableView)消息发送到NSString,那么您将收到类似的异常。这是因为NSString没有实现(有一个方法)reloadData

此错误经常发生在prepareForSegue:sender:方法中,因为您将UIViewController类型转换为自定义子类(在本例中为CommentsViewControllerNew)。类型转换发生在这一行:

CommentsViewControllerNew *commentsVC = (CommentsViewControllerNew *)[segue destinationViewController];

你基本上是在告诉编译器:是的我知道你认为这是UIViewController,但它实际上是CommentsViewControllerNew。崩溃的原因是,在这种情况下编译器是正确的, a UIViewControllerUIViewController没有方法或者属性名称为questionObjectID

经过长时间的解释......解决方法是转到Interface Builder,选择相关的视图控制器并将其类设置为CommentsViewControllerNew

注意:如果您不了解或不熟悉我正在谈论的任何内容,我建议您做一些阅读或做一些教程。 YouTube上有很多好的。