获取“无法识别的选择器发送到实例”以编程方式添加的 UIButton 触摸事件发生错误

发布于 2025-01-06 04:37:36 字数 2049 浏览 0 评论 0原文

我有一个显示内容列表的 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 技术交流群。

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

发布评论

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

评论(1

满意归宿 2025-01-13 04:37:36

您正在注册选择器 checkBoxButton:,但实际上正在实现 checkboxButton: (请注意第一个“B”的大小写差异)。

You're registering the selector checkBoxButton: but you're actually implementing checkboxButton: (note the difference in capitalization of the first "B").

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