我需要为单个 UIButton 传递两个动作。
I need to pass two actions for single UIButton.
第一个参数成功传递如下:
First argument passed successfully asa follows:
[imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside]; imageButton.tag = 1;但我需要为同一个按钮传递另一个参数:
But i need to pass another argument also for same button:
int secondAction =10; [imageButton addTarget:self action:@selector(imageClicked:*secondAction) forControlEvents:UIControlEventTouchUpInside];任何人都可以帮助如何为单个按钮/选择器传递两个值。
Can anybody help how to pass two values for single button/selector.
推荐答案您可以使用 Objective C Runtime 功能将数据与对象关联为:
You can use Objective C Runtime feature for associating data with objects as :
步骤1:在您的班级中导入: #import< objc / runtime.h>
Step 1 : Import this in your class : #import <objc/runtime.h>
步骤2:将密钥名称改为: static char * kDataAssociationKey =associated_data_key;
step 2 : Crete a key name as : static char * kDataAssociationKey = "associated_data_key";
步骤3:将数据与您的对象(例如:按钮)关联为:
Step 3 : Associate data with your object (Ex: button) as :
NSString *your_data =@"Data which is being associated"; objc_setAssociatedObject(imageButton, kDataAssociationKey, your_data, OBJC_ASSOCIATION_RETAIN);第4步:获取方法中的关联数据:
Step 4 : Get associated data in your method as :
NSString *value = (NSString *)objc_getAssociatedObject(imageButton, kDataAssociationKey);希望对您有帮助。
更多推荐
对于UIButton,如何通过@selector传递多个参数
发布评论