发送到实例的无法识别的选择器“问题
我的代码在途中某处中断,并在使用导航栏按钮时崩溃。
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 $的对象c $ c>协议。
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 $ c停留的时间不变。 $ c>需要它。
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.