无法识别的选择器发送到实例"问题 [英] Unrecognized selector sent to instance" problem

查看:90
本文介绍了无法识别的选择器发送到实例"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在过程中的某个地方出错了,并且在使用导航栏按钮时崩溃了.

my code broke somewhere along the way, and crashes when using the navigation bar buttons.

错误信息:*** 由于未捕获的异常NSInvalidArgumentException"而终止应用,原因:-[UIView newMemoViewController:didAddMemo:]: 无法识别的选择器发送到实例 0x5b55a60'

调试时,程序确实运行了cancel方法,并在@synthesize行抛出异常.但是,我看不出有什么问题.

When debugging, the program does run the cancel method, and throws an exception at the @synthesize line. However, I cannot see anything wrong with it.

症状是相同的,所以我只包括Cancel按钮的相关代码:

The symptoms are identical, so I am including the relevant code only for the Cancel button:

NewMemoViewController.h

#import <UIKit/UIKit.h>
@protocol NewMemoDelegate;


@class AKVoiceMemo;


@interface NewMemoViewController : UIViewController {
    @private
        AKVoiceMemo *voiceMemo;
        id <NewMemoDelegate> delegate;
}

@property (nonatomic, retain) AKVoiceMemo *voiceMemo;
@property (nonatomic, assign) id <NewMemoDelegate> delegate;

@end

@protocol NewMemoDelegate <NSObject>
- (void)newMemoViewController:(NewMemoViewController *)newMemoViewController didAddMemo:(AKVoiceMemo *)voiceMemo;


@end

NewMemoViewController.m

#import "NewMemoViewController.h"

@synthesize delegate;


- (void)viewDidLoad {
    UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = cancelButtonItem;
    [cancelButtonItem release];
}


- (void)cancel {
    [self.delegate newMemoViewController:self didAddMemo:nil];
}

您的帮助将不胜感激.

编辑:委托是RootViewController:

- (void)newMemoViewController:(NewMemoViewController *)newMemoViewController didAddMemo:(AKVoiceMemo *)voiceMemo {
    if (voiceMemo){
        // Show the note in a new view controller
        // TODO: Implement this
    }

    [self dismissModalViewControllerAnimated:YES];
}

推荐答案

您可能将 NewMemoViewController 的委托设置为 UIView 对象而不是实现NewMemoDelegate 协议.

You're probably setting the delegate of NewMemoViewController to a UIView object instead of an object that implements the NewMemoDelegate protocol.

错误消息告诉您 newMemoViewController:didAddMemo: 消息已发送到 UIView 对象而 UIView 对象没有知道如何处理它.由于您的 cancel 方法在委托上调用 newMemoViewController:didAddMemo:,因此 delegateUIView 对象无法识别 newMemoViewController:didAddMemo: 消息.换句话说,您的委托是一个 UIView 并且它没有实现 NewMemoDelegate 协议.

The error message is telling you that a newMemoViewController:didAddMemo: message was sent to a UIView object and the UIView object didn't know what to do with it. Since your cancel method calls newMemoViewController:didAddMemo: on the delegate, it is the delegate which is the UIView object that doesn't recognize the newMemoViewController:didAddMemo: message. In other words, your delegate is a UIView and it doesn't implement the NewMemoDelegate protocol.

如果您正确设置了委托,那么@jtbandes 说明了一个很好的观点:委托可能正在被释放并且 UIView 对象正在接管相同的内存位置,从而成为"委托意外地.通过为您的委托使用 assign 属性,您正在做正确的事情;这是相当标准的可可做法.但是,您确实需要确保委托被另一个对象保留,并且 那个 对象需要确保委托一直存在,只要 NewMemoViewController 需要它.

If you are correctly setting the delegate, then @jtbandes makes a great point: The delegate is probably being released and a UIView object is taking over the same memory location, thus "becoming" the delegate by accident. You're doing the right thing by using the assign attribute for your delegate; that's fairly standard Cocoa practice. However, you do need to make sure that the delegate is retained by another object, and that object needs to make sure that the delegate sticks around as long as NewMemoViewController needs it to.

这篇关于无法识别的选择器发送到实例&quot;问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆