从 UIButton 发送到实例错误消息的无法识别的选择器

发布于 2024-11-07 14:49:27 字数 917 浏览 0 评论 0原文

我有一个以编程方式添加到表视图中的 UIButton。问题是,当触摸它时,我遇到了发送到实例的无法识别的选择器错误消息。

    UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeInfoDark];     
    [alertButton addTarget:self.tableView action:@selector(showAlert:) 
          forControlEvents:UIControlEventTouchUpInside];
    alertButton.frame = CGRectMake(220.0, 20.0, 160.0, 40.0);

    [self.tableView addSubview:alertButton];

这是我想在触摸 InfoDark UIButton 时触发的警报方法:

- (void) showAlert {
        UIAlertView *alert = 
         [[UIAlertView alloc] initWithTitle:@"My App" 
                                    message: @"Welcome to ******. \n\nSome Message........" 
                                   delegate:nil 
                          cancelButtonTitle:@"Dismiss" 
                          otherButtonTitles:nil];
        [alert show];
        [alert release];
}

感谢您的帮助。

I have a UIButton that is added to a tableview programmatically. The problem is that when it is touched I run into the unrecognized selector sent to instance error message.

    UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeInfoDark];     
    [alertButton addTarget:self.tableView action:@selector(showAlert:) 
          forControlEvents:UIControlEventTouchUpInside];
    alertButton.frame = CGRectMake(220.0, 20.0, 160.0, 40.0);

    [self.tableView addSubview:alertButton];

and here's the alert method which I want to trigger when the InfoDark UIButton is touched:

- (void) showAlert {
        UIAlertView *alert = 
         [[UIAlertView alloc] initWithTitle:@"My App" 
                                    message: @"Welcome to ******. \n\nSome Message........" 
                                   delegate:nil 
                          cancelButtonTitle:@"Dismiss" 
                          otherButtonTitles:nil];
        [alert show];
        [alert release];
}

thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

别低头,皇冠会掉 2024-11-14 14:49:27

好吧,你有两个问题。
一是如上所述的选择器问题,但您真正的问题是:

[alertButton addTarget:self.tableView 
                action:@selector(showAlert:) 
      forControlEvents:UIControlEventTouchUpInside];

这是错误的目标,除非您已子类化 UITableView 来响应警报。

您想将该代码更改为:

[alertButton addTarget:self 
                action:@selector(showAlert) 
      forControlEvents:UIControlEventTouchUpInside];

Ok you have two problems.
one is the selector issue as stated above, but your real problem is:

[alertButton addTarget:self.tableView 
                action:@selector(showAlert:) 
      forControlEvents:UIControlEventTouchUpInside];

This is the wrong target, unless you have subclassed UITableView to respond to the alert.

you want to change that code to:

[alertButton addTarget:self 
                action:@selector(showAlert) 
      forControlEvents:UIControlEventTouchUpInside];
︶ ̄淡然 2024-11-14 14:49:27

崩溃原因:您的showAlert函数原型必须是- (void) showAlert:(id) sender

使用下面的代码

- (void) showAlert:(id) sender {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My App" message: @"Welcome to ******. \n\nSome Message........" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];
}

正如 Jacob Relkin 在他的回答中所说这里< /a>:

因为您在其中包含了冒号 (:)
addTarget 的选择器参数,
接收选择器必须接受
范围。运行时没有
识别选择器
@selector(buttonTouched:),因为
没有具有该名称的方法
接受一个参数。改变
接受参数的方法签名
类型 id 来解决此问题。

The reason of Crash : your showAlert function prototype must be - (void) showAlert:(id) sender.

Use below code

- (void) showAlert:(id) sender {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My App" message: @"Welcome to ******. \n\nSome Message........" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];
}

As Jacob Relkin says in his answer here:

Because you included a colon (:) in
your selector argument to addTarget,
the receiving selector must accept a
parameter. The runtime doesn't
recognize the selector
@selector(buttonTouched:), because
there isn't a method with that name
that accepts a parameter. Change the
method signature to accept a parameter
of type id to resolve this issue.

旧情勿念 2024-11-14 14:49:27

Jhaliya 是正确的,但这里有一个简短的解释。

当您配置按钮的目标时,您定义了如下所示的选择器:

@selector( showAlert: )

冒号 (:) 为需要一个参数的选择器建立了一种方法签名。但是,您的方法被定义为 -showAlert,不带任何参数,因此您的对象实际上并未实现您告诉 UIButton 调用的方法。重新定义您的方法(如 Jhaliya 所示)将会起作用,将按钮目标的选择器更改为:

@selector( showAlert )

Jhaliya is correct, but here's a brief explanation of why.

When you configured the button's target, you defined the selector like this:

@selector( showAlert: )

The colon (:) establishes a method signature for the selector requiring one argument. However, your method was defined as -showAlert, taking no arguments, so your object did not actually implement the method you told the UIButton to invoke. Redefining your method as shown by Jhaliya will work, as will changing your button target's selector to:

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