对于UIButton,如何通过@selector传递多个参数

编程入门 行业动态 更新时间:2024-10-07 01:33:44
本文介绍了对于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传递多个参数

本文发布于:2023-11-23 21:47:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   参数   UIButton   selector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!