获取“无法识别的选择器发送到实例”以编程方式添加的 UIButton 触摸事件发生错误
我有一个显示内容列表的 UITableView,我希望列表中的每个项目都有一个复选框,可以通过用户触摸来标记和取消标记。我为每个单元格创建了一个UIButton,将其设置为单元格的accessoryView,并添加了要调用的目标方法。
但是,每当我尝试单击该复选框时,我总是会收到“无法识别的选择器发送到实例”错误,但我不知道为什么。我到处查看以找出导致错误的原因,并确保我的 addTarget 调用和所选方法使用正确的语法,但也许我遗漏了一些东西。我需要更改什么来修复此错误?
这是创建单元格的代码:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// Set up the cell...
PFObject *tempMap = [searchResults objectAtIndex: [indexPath row]];
cell.textLabel.text = [tempMap objectForKey:@"mapName"];
// Add checkbox to cell
UIButton *checkBox = [UIButton buttonWithType:UIButtonTypeCustom];
checkBox.bounds = CGRectMake(0, 0, 30, 30);
cell.accessoryView = checkBox;
checkBox.tag = indexPath.row;
[checkBox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkBox setImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkBox setImage:[UIImage imageNamed:@"checkbox-pressed.png"] forState:UIControlStateHighlighted];
[checkBox addTarget:self action:@selector(checkBoxButton:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkBox];
}
return cell;
}
这是正在调用的方法,checkBoxButton:
- (void)checkboxButton:(id)sender
{
UIButton *checkBox = sender;
if (checkBox.selected)
{
[selectedMaps removeObject:[searchResults objectAtIndex:checkBox.tag]];
NSLog(@"..Map Deselected..");
}
else
{
[selectedMaps addObject:[searchResults objectAtIndex:checkBox.tag]];
NSLog(@"..Map Selected..");
}
}
I have a UITableView displaying a list of content, and I want each item in the list to have a checkbox that can be marked and unmarked by user touch. I have created a UIButton for each cell, set it to the cell's accessoryView, and added the target method to be called.
However, I always get the "unrecognized selector sent to instance" error whenever I attempt to click on the checkbox, and I have no idea why. I looked all over the place to figure out what's causing the error, and made sure my addTarget call and selected method are using proper syntax, but maybe I'm missing something. What do I need to change to fix this error?
Here is the code for creating the cells:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// Set up the cell...
PFObject *tempMap = [searchResults objectAtIndex: [indexPath row]];
cell.textLabel.text = [tempMap objectForKey:@"mapName"];
// Add checkbox to cell
UIButton *checkBox = [UIButton buttonWithType:UIButtonTypeCustom];
checkBox.bounds = CGRectMake(0, 0, 30, 30);
cell.accessoryView = checkBox;
checkBox.tag = indexPath.row;
[checkBox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkBox setImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkBox setImage:[UIImage imageNamed:@"checkbox-pressed.png"] forState:UIControlStateHighlighted];
[checkBox addTarget:self action:@selector(checkBoxButton:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkBox];
}
return cell;
}
This is the method that's being called, checkBoxButton:
- (void)checkboxButton:(id)sender
{
UIButton *checkBox = sender;
if (checkBox.selected)
{
[selectedMaps removeObject:[searchResults objectAtIndex:checkBox.tag]];
NSLog(@"..Map Deselected..");
}
else
{
[selectedMaps addObject:[searchResults objectAtIndex:checkBox.tag]];
NSLog(@"..Map Selected..");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在注册选择器
checkBoxButton:
,但实际上正在实现checkboxButton:
(请注意第一个“B”的大小写差异)。You're registering the selector
checkBoxButton:
but you're actually implementingcheckboxButton:
(note the difference in capitalization of the first "B").