加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何在UIButton中实现addTarget属性

发布时间:2020-12-14 19:57:22 所属栏目:百科 来源:网络整理
导读:我有一个函数(例如function1),我有一个按钮和一个名为auxiliarStruct的对象,我有这条线: [btn addTarget:self action:@selector(userSelected:) forControlEvents:UIControlEventTouchUpInside]; 但是在@selector(userSelected)中我需要将该函数中所需的对
我有一个函数(例如function1),我有一个按钮和一个名为auxiliarStruct的对象,我有这条线:

[btn addTarget:self action:@selector(userSelected:) forControlEvents:UIControlEventTouchUpInside];

但是在@selector(userSelected)中我需要将该函数中所需的对象作为参数传递,但我不知道如何实现它.
我在这里声明了这个对象:

//UsersController.h
#import <UIKit/UIKit.h>

@interface UsersController : NSObject{
    NSInteger *prof_id;
    NSString *createTime;
    NSString *fullName;
    NSString *thumb;
}

@property (nonatomic) NSInteger *prof_id;
@property (nonatomic,retain) IBOutlet NSString *createTime;
@property (nonatomic,retain) IBOutlet NSString *fullName;
@property (nonatomic,retain) IBOutlet NSString *thumb;

@end

我有这样声明的被调用函数:

-(void)userSelected:(id)sender{
  //CODE
}

我有一个这个类的对象,叫做auxiliarStruct,这就是我需要的.我试过了

[btn addTarget:self action:@selector(**userSelected:auxiliarStruct**)forControlEvents:UIControlEventTouchUpInside];

但它不起作用
谁能帮我?谢谢.

ps:对不起我的英语,我知道这很糟糕

解决方法

您只能使用addTarget @selector方法将(UIButton *)对象作为参数传递.您可以设置UIButton标记并在被调用函数上使用它.您可能想要找出发送字符串值的任何替代方法.

使用以下代码通过按钮传递标记:

btn_.tag = 10; 
    [btn_ addTarget:self action:@selector(functionName:) forControlEvents:UIControlEventTouchUpInside];

按钮操作应如下所示:

- (void) functionName:(UIButton *) sender {

    NSLog(@"Tag : %d",sender.tag);

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读