Objective c 以编程方式调用按钮的操作会导致NSInvalidArgumentException

Objective c 以编程方式调用按钮的操作会导致NSInvalidArgumentException,objective-c,uitableview,uibutton,selector,performselector,Objective C,Uitableview,Uibutton,Selector,Performselector,我有一个tableview每个单元格包含一个文本和一个按钮。对于cellforrowatinexpath中每个单元格中的每个按钮,我使用选择器定义了一个操作: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ProductTableViewCell *cell = [tableView dequeueReusableCellWi

我有一个
tableview
每个单元格包含一个文本和一个按钮。对于
cellforrowatinexpath
中每个单元格中的每个按钮,我使用选择器定义了一个操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCell" forIndexPath:indexPath];

    cell.stateButton.tag = indexPath.row;

    [cell.stateButton addTarget:self action:@selector(stateChange:) forControlEvents:UIControlEventTouchUpInside];

    /// Rest of code

    return cell;
}
在流程中的某个时刻,我希望为我的
表视图中的所有按钮生成一个操作。我是这样做的:

#pragma mark - Delegate
- (void)onAllSwitchesStateChange{

    for(int i = 0; i < [[SsrDeviceManager instance] ssrDevices].count; i ++){

        ProductTableViewCell* cell = (ProductTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        UIButton* stateButton = [cell stateButton];
        [stateButton performSelector:@selector(stateChange:) withObject:nil];

    }
}
但是,为什么我尝试运行此代码,我得到一个错误,应用程序崩溃,并显示以下消息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton stateChange:]: unrecognized selector sent to instance 0x1023686d0'
*** First throw call stack:
(0x233133180 0x23230b9f8 0x23304f9bc 0x25f94c220 0x2331389c8 0x23313a65c 0x101073bdc 0x100fdd844 0x25f922300 0x25f3cb424 0x25f3cb744 0x25f3ca7b0 0x25f9595c4 0x25f95a7ec 0x25f93a85c 0x25fa009d4 0x25fa03100 0x25f9fc330 0x2330c4f1c 0x2330c4e9c 0x2330c4784 0x2330bf6c0 0x2330befb4 0x2352c079c 0x25f920c38 0x10102b794 0x232b828e0)
如何一次为所有按钮生成单击操作

“如何一次为所有按钮生成单击操作”

这个概念本身毫无意义。表视图就是视图。它允许用户与您(应用程序)通信,因为它是可触摸的界面,但它本身不是活动的来源;它只是表示数据

如果你想让事情发生,那么打开你所有的按钮,让他们都开始某种行动是疯狂的。只需对您的数据、您自己以及您想做的任何事情进行处理

“如何一次为所有按钮生成单击操作”

这个概念本身毫无意义。表视图就是视图。它允许用户与您(应用程序)通信,因为它是可触摸的界面,但它本身不是活动的来源;它只是表示数据


如果你想让事情发生,那么打开你所有的按钮,让他们都开始某种行动是疯狂的。只需自己对数据执行任何您想执行的操作。

UIButton
没有方法
stateChange:
。您的类有一个方法
stateChange:
。您可以调用自己类的方法,如
[self stateChange:stateButton]
。如果您想让按钮发送在发生特定类型的事件时它将发送的消息,您可以执行类似于
[状态按钮发送操作控制事件:uicontrolTouchUpInside]
UIButton
没有方法
状态更改:
。您的类有一个方法
stateChange:
。您可以调用自己类的方法,如
[self stateChange:stateButton]
。如果希望按钮发送在发生特定类型的事件时它将发送的消息,可以执行类似于
[stateButton SendActions ForControlEvents:UIControlEventTouchUpInside]
的操作
UITableViewCell
对象不是视图控制器。如果你想让一个按钮起作用,你必须让一个视图控制器来做这项工作。
stateChange
不是
UIButton
@PhillipMills的方法,我知道不是。这是当用户单击按钮时我调用的方法。我想在所有按钮上调用该方法;您可以对它所属的任何对象(视图控制器?)调用它,并将该按钮作为参数传递。谢谢。有可能达到我想要的行为吗?总之,我想在所有按钮上生成单击操作。
UITableViewCell
对象不是视图控制器。如果你想让一个按钮起作用,你必须让一个视图控制器来做这项工作。
stateChange
不是
UIButton
@PhillipMills的方法,我知道不是。这是当用户单击按钮时我调用的方法。我想在所有按钮上调用该方法;您可以对它所属的任何对象(视图控制器?)调用它,并将该按钮作为参数传递。谢谢。有可能达到我想要的行为吗?总之,我想在所有按钮上生成单击操作。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton stateChange:]: unrecognized selector sent to instance 0x1023686d0'
*** First throw call stack:
(0x233133180 0x23230b9f8 0x23304f9bc 0x25f94c220 0x2331389c8 0x23313a65c 0x101073bdc 0x100fdd844 0x25f922300 0x25f3cb424 0x25f3cb744 0x25f3ca7b0 0x25f9595c4 0x25f95a7ec 0x25f93a85c 0x25fa009d4 0x25fa03100 0x25f9fc330 0x2330c4f1c 0x2330c4e9c 0x2330c4784 0x2330bf6c0 0x2330befb4 0x2352c079c 0x25f920c38 0x10102b794 0x232b828e0)