ios - 尝试处理来自 UITableView 单元格中 UIButton 的点击事件会导致无效的选择器错误

标签 ios objective-c uitableview

我正在尝试处理 UITableView 单元格中 UIButton 的点击事件,但不幸的是应用程序崩溃并显示错误消息:

unrecognized selector sent to instance 0x7f95984cbaf0

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // this is a simplified version of this method for brevity.
    NSMutableArray* views = [NSMutableArray array];

    UIButton* button = [UIButton buttonWithThinBorderedColor_QAN:[UIColor colorForPrimaryUse]
                                                highlightedColor:nil
                                                   disabledColor:nil];
    [button addTarget:self action:@selector(showSomething) forControlEvents:UIControlEventTouchUpInside];

    button.tag = indexPath.row;
    [views addObject:button];
    [cell setContents:views withConfiguration:configuration];
    return cell;
}

在 myViewController.h(简化版)中我有:

@interface SEFSFormEditingViewController : UITableViewController

- (void) showSomething;

@end

在 myViewController.m 中我有:

-(void)showSomething:(UIButton *)sender
{
    if (sender.tag == 0) {
    NSString* test= @"test";
    }
}

当我运行应用程序时,按钮会正确显示,但当我点击它时,应用程序会崩溃。

有人可以指出解决此问题的正确方向吗?

最佳答案

替换您的以下代码行

[button addTarget:self action:@selector(showSomething) forControlEvents:UIControlEventTouchUpInside];

[button addTarget:self action:@selector(showSomething:) forControlEvents:UIControlEventTouchUpInside];

关于ios - 尝试处理来自 UITableView 单元格中 UIButton 的点击事件会导致无效的选择器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40083759/

相关文章:

ios - 更新另一个类别 objective-c 的变量

ios - Localizable.strings 在 iOS 框架中不起作用

uitableview - Monotouch.Dialog:在 UITableView 中启用选择

ios - swift 将元组作为 AnyObject 传递

ios - 这个 Rx 代码是否订阅了 JSON 本身的变化?

ios - Swift iOS - 将数组过滤成类似对象的数组

ios - MPMoviePlayerViewController 状态栏不隐藏

objective-c - 函数调用的参数太多,预期为 0 method_invoke

ios - 使用 resizableImageWithCapInsets 崩溃 :UIEdgeInsetsMake

ios - heightForHeaderInSection 只调用一次