[英]Alternative option of UIButton addTarget:action:forControlEvents: method in IOS
I know that this question has been asked so many times but i want to pass a custom object as an argument on clicking a button. 我知道这个问题已经被问了很多次,但是我希望在单击按钮时传递一个自定义对象作为参数。
UIButton addTarget:action:forControlEvents:
doesn't allow us to do that but it is important for me so I can do further on the basis of custom objects. UIButton addTarget:action:forControlEvents:
不允许我们这样做但对我来说很重要,所以我可以在自定义对象的基础上做进一步的工作。
If an alternative for add target is possible, so please give the solution. 如果可以选择添加目标,请提供解决方案。
The code is like this: 代码是这样的:
Custom Object: 自定义对象:
HeaderData *cell ;
Button: 按钮:
_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];
and the function which is called on clicking the UIButton _forward is: 单击UIButton _forward时调用的函数是:
-(void)functionName:(UIButton *)sender{
//code for further programming on the basis of cell.
}
You could make a custom subclass of UIButton: 您可以创建UIButton的自定义子类:
@interface CustomDataButton : UIButton
@property (nonatomic, strong) id userData;
@end
Then in functionName, you can pull the data back out: 然后在functionName中,您可以将数据拉回:
-(void)functionName:(UIButton *)sender
{
if ([sender isKindOfClass:[CustomDataButton class]])
{
id customData = ((CustomDataButton *) sender).userData;
}
}
caveat : written without an IDE. 警告 :没有IDE编写。 Watch out for typos etc.
注意拼写错误等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.