按 UIButton 会导致无法识别的选择器错误

发布于 2024-12-02 22:42:02 字数 855 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

傲世九天 2024-12-09 22:42:02

选择器的正确名称是

@selector(toggleEdit:)

没有 : 它会寻找具有此签名的方法:

-(void) toggleEdit  // No parameters
{
}

当您实际声明时:

-(void) toggleEdit:(id)sender
{
}

The correct name for your selector is

@selector(toggleEdit:)

Without the : it would look for a method with this signature:

-(void) toggleEdit  // No parameters
{
}

When you actually have declared:

-(void) toggleEdit:(id)sender
{
}
心奴独伤 2024-12-09 22:42:02

更改

@selector(toggleEdit)

@selector(toggleEdit:)

Change

@selector(toggleEdit)

to

@selector(toggleEdit:)
暮光沉寂 2024-12-09 22:42:02

对于其他遇到此问题的人,当您不再拥有某些东西并且该语句指向内存中的某个垃圾地址时,您也可能会收到“无法识别的选择器发送到实例 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.

想你的星星会说话 2024-12-09 22:42:02

应该是:

-(IBAction) toggleEdit:(id)sender {}

@selector(toggleEdit:)

should be:

-(IBAction) toggleEdit:(id)sender {}

and

@selector(toggleEdit:)
倥絔 2024-12-09 22:42:02

尝试:

UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self action:@selector(toggleEdit) forControlEvents:UIControlEventTouchUpInside];

Try:

UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self action:@selector(toggleEdit) forControlEvents:UIControlEventTouchUpInside];
樱&纷飞 2024-12-09 22:42:02

您尝试使用的方法需要一个参数,这意味着它的名称中有一个冒号 - 冒号实际上是名称的一部分。获取选择器时需要包含该冒号:

@selector(toggleEdit:)

请参阅 消息语法 部分:

选择器名称包含名称的所有部分,包括冒号,因此上例中的选择器名为 setOriginX:y:。它有两个冒号,因为它需要两个参数。但是,选择器名称不包含任何其他内容,例如返回类型或参数类型。

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:

@selector(toggleEdit:)

Refer to the Message Syntax section of The Objective-C Programming Language:

A selector name includes all the parts of the name, including the colons, so the selector in the preceding example is named setOriginX:y:. It has two colons, because it takes two parameters. The selector name does not, however, include anything else, such as return type or parameter types.

薆情海 2024-12-09 22:42:02

如果您使用故事板。有时,从故事板中删除按钮并放置一个新按钮并进行必要的连接会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文