简体   繁体   English

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

[英]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.

相关问题 使用 addTarget:action:forControlEvents: 方法更改 UIImage? - Use addTarget:action:forControlEvents: method to change a UIImage? 具有自定义视图的UIButton-addTarget:action:forControlEvents:不起作用 - UIButton with custom view - addTarget:action:forControlEvents: does not work SubClassing UIButton但我不能让“addTarget:action:forControlEvents:”执行? - SubClassing UIButton but I can't get “addTarget:action:forControlEvents:” to execute? addTarget:action:forControlEvents:无法识别的选择器发送到实例-uitablecell中的uibutton - addTarget:action:forControlEvents: unrecognized selector sent to instance - uibutton in uitablecell UIButton addTarget:action:forControlEvents:不能将自己用作目标吗? - UIButton addTarget:action:forControlEvents: not working using itself as a target? 目标C:如何使用addTarget:action:forControlEvents:方法? - Objective C: How to use addTarget:action:forControlEvents: method? 如何从类方法调用 addTarget:action:forControlEvents:? - How to call addTarget:action:forControlEvents: from a class method? 将参数传递给 addTarget:action:forControlEvents - Passing parameters to addTarget:action:forControlEvents 在.xib文件中创建的UIButton是IBAction还是[UIButton addTarget:action:forControlEvents:]? 哪种方法更好? - IBAction or [UIButton addTarget:action: forControlEvents:] for UIButton created in .xib file? Which is the better approach? 如何将多个标签传递给addTarget:action:forControlEvents? - how to pass multiple tags to addTarget:action:forControlEvents?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM