gpt4 book ai didi

ios - UIButton addTarget 的替代选项 :action:forControlEvents: method in IOS

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:08 24 4
gpt4 key购买 nike

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

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 的情况下编写。注意错别字等。

关于ios - UIButton addTarget 的替代选项 :action:forControlEvents: method in IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31830317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com