按 UIButton 会导致无法识别的选择器错误
我的 UIView
中有一个按钮,其创建方式如下:
UIBarButtonItem *editButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(toggleEdit)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
这是操作方法:
-(void) toggleEdit:(id)sender
{
}
但我收到此错误
2011-09-02 15:27:13.362 blubb[15006:207] -[DatabaseSelectionViewControllertoggleEdit]:无法识别的选择器发送到实例 0x5a29d80 2011-09-02 15:27:13.365 blubb[15006:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[DatabaseSelectionViewControllertoggleEdit]:无法识别的选择器发送到实例 0x5a29d80”
为什么会发生这种情况?
I have a button in my UIView
that is created like so:
UIBarButtonItem *editButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(toggleEdit)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
And this is the action method:
-(void) toggleEdit:(id)sender
{
}
but I get this error
2011-09-02 15:27:13.362 blubb[15006:207] -[DatabaseSelectionViewController toggleEdit]: unrecognized selector sent to instance 0x5a29d80
2011-09-02 15:27:13.365 blubb[15006:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DatabaseSelectionViewController toggleEdit]: unrecognized selector sent to instance 0x5a29d80'
Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
选择器的正确名称是
没有 : 它会寻找具有此签名的方法:
当您实际声明时:
The correct name for your selector is
Without the : it would look for a method with this signature:
When you actually have declared:
更改
为
Change
to
对于其他遇到此问题的人,当您不再拥有某些东西并且该语句指向内存中的某个垃圾地址时,您也可能会收到“无法识别的选择器发送到实例 0x...”的错误。
For anyone else stumbling across this, you can also receive those "unrecognized selector sent to instance 0x..." errors when you no longer own something and the statement is pointing to some junk address in memory.
应该是:
和
should be:
and
尝试:
Try:
您尝试使用的方法需要一个参数,这意味着它的名称中有一个冒号 - 冒号实际上是名称的一部分。获取选择器时需要包含该冒号:
请参阅 消息语法 部分:
The method you're trying to use takes an argument, which means that it has a colon in its name -- the colon is actually part of the name. You need to include that colon when you get the selector:
Refer to the Message Syntax section of The Objective-C Programming Language:
如果您使用故事板。有时,从故事板中删除按钮并放置一个新按钮并进行必要的连接会有所帮助。
If you are using a storyboard. Sometimes it helps to remove the button from the storyboard, and put a new button, and make the necessary connections.