对于UIButton,如何通过@selector传递多个参数 [英] For UIButton how to pass multiple arguments through @selector

查看:672
本文介绍了对于UIButton,如何通过@selector传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为单个 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传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆