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

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

问题描述

我的代码在途中某处中断,并在使用导航栏按钮时崩溃。

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.

症状是相同的,因此我只包含有关取消按钮的相关代码:

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 对象不知道该如何处理。由于您的取消方法在委托上调用 newMemoViewController:didAddMemo:,因此它是委托这是 UIView 对象,无法识别 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.

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

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