Ios 无法识别的选择器从UIButton发送到实例错误消息
我有一个UIButton,它以编程方式添加到tableview中。问题是,当触摸它时,我会遇到发送到实例的无法识别的选择器错误消息Ios 无法识别的选择器从UIButton发送到实例错误消息,ios,objective-c,iphone,uibutton,unrecognized-selector,Ios,Objective C,Iphone,Uibutton,Unrecognized Selector,我有一个UIButton,它以编程方式添加到tableview中。问题是,当触摸它时,我会遇到发送到实例的无法识别的选择器错误消息 UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeInfoDark]; [alertButton addTarget:self.tableView action:@selector(showAlert:) forControlEvents:UICo
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按钮时触发的警报方法:
- (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];
}
感谢您的帮助。崩溃原因:您的
showarter
函数原型必须是-(void)showarter:(id)发送方
使用下面的代码
- (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];
}
正如雅各布·雷金在回答中所说:
因为您在中包含了冒号(:)
将选择器参数添加到addTarget,
接收选择器必须接受
参数运行时没有
识别选择器
@选择器(按钮触动:),因为
没有具有该名称的方法
它接受一个参数。改变
方法签名以接受参数
用于解决此问题的id类型
贾利亚是正确的,但这里有一个简短的解释 配置按钮的目标时,您定义了如下选择器:
@selector( showAlert: )
冒号(:)为需要一个参数的选择器建立方法签名。但是,您的方法被定义为-showart
,不带任何参数,因此您的对象实际上没有实现您告诉ui按钮调用的方法。重新定义Jhaliya所示的方法将起作用,将按钮目标的选择器更改为:
@selector( showAlert )
好的,你有两个问题。
一个是如上所述的选择器问题,但您真正的问题是:
[alertButton addTarget:self.tableView
action:@selector(showAlert:)
forControlEvents:UIControlEventTouchUpInside];
这是错误的目标,除非您有子类UITableView来响应警报
您想将该代码更改为:
[alertButton addTarget:self
action:@selector(showAlert)
forControlEvents:UIControlEventTouchUpInside];
我将showAlert方法更改为showAlert:(id)发件人。操作:@selector(showAlert:)是否有任何更改?感谢you@hanumanDev:仅更改为showAlert:(id)sender,让您的操作按它的方式进行(不要更改操作:@selector(showAlert:)。我这样做了,只更改了showAlert:(id)sender(在.h和.m文件中),但它仍然崩溃。我改为在IB中执行此操作,并将此方法更改为iAction,它可以正常工作。我看不出我犯了什么错。谢谢你的帮助。谢谢你的解释。我尝试了@selector(showAlert),但它仍然会因无法识别的选择器错误而崩溃。我一定是做错了什么。