从 UIButton 发送到实例错误消息的无法识别的选择器
我有一个以编程方式添加到表视图中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,你有两个问题。
一是如上所述的选择器问题,但您真正的问题是:
这是错误的目标,除非您已子类化 UITableView 来响应警报。
您想将该代码更改为:
Ok you have two problems.
one is the selector issue as stated above, but your real problem is:
This is the wrong target, unless you have subclassed UITableView to respond to the alert.
you want to change that code to:
崩溃原因:您的
showAlert
函数原型必须是- (void) showAlert:(id) sender
。使用下面的代码
正如 Jacob Relkin 在他的回答中所说这里< /a>:
The reason of Crash : your
showAlert
function prototype must be- (void) showAlert:(id) sender
.Use below code
As Jacob Relkin says in his answer here:
Jhaliya 是正确的,但这里有一个简短的解释。
当您配置按钮的目标时,您定义了如下所示的选择器:
冒号 (:) 为需要一个参数的选择器建立了一种方法签名。但是,您的方法被定义为
-showAlert
,不带任何参数,因此您的对象实际上并未实现您告诉UIButton
调用的方法。重新定义您的方法(如 Jhaliya 所示)将会起作用,将按钮目标的选择器更改为:Jhaliya is correct, but here's a brief explanation of why.
When you configured the button's target, you defined the selector like this:
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 theUIButton
to invoke. Redefining your method as shown by Jhaliya will work, as will changing your button target's selector to: