UIButton 的替代选项 addTarget:action:forControlEvents: IOS 中的方法

Alternative option of UIButton addTarget:action:forControlEvents: method in IOS

我知道这个问题已被问过很多次,但我想在单击按钮时将自定义对象作为参数传递。

UIButton addTarget:action:forControlEvents: 不允许我们这样做,但这对我很重要,因此我可以在自定义对象的基础上做进一步的工作。

如果可以替代添加目标,请给出解决方案。

代码是这样的:

自定义对象:

HeaderData *cell ;

按钮:

_forward =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[_forward setTitle:@"F" forState:UIControlStateNormal];
_forward.frame = CGRectMake(300, (_CELL_HEIGHT-_LABEL_HEIGHT)/2, 10 ,_LABEL_HEIGHT);]
[_forward addTarget:web action:@selector(functionName:) forControlEvents:UIControlEventTouchUpInside]; 

单击 UIButton _forward 时调用的函数是:

-(void)functionName:(UIButton *)sender{
    //code for further programming on the basis of cell.
}

您可以创建 UIButton 的自定义子类:

@interface CustomDataButton : UIButton
@property (nonatomic, strong) id userData;
@end

然后在functionName中,可以拉回数据:

-(void)functionName:(UIButton *)sender 
{
    if ([sender isKindOfClass:[CustomDataButton class]])
    {
        id customData = ((CustomDataButton *) sender).userData;
    }
}

警告:没有 IDE。注意错别字等