简体   繁体   English

如何将选择器(SEL)与objc_setAssociatedObject一起使用?

[英]How to use selector (SEL) with objc_setAssociatedObject?

Can I use somehow parameter of type SEL with objc_setAssociatedObject without converting it to NSString type? 我可以使用objc_setAssociatedObject以某种方式使用SEL类型的参数而不将其转换为NSString类型吗?

Currently I'm using this code: objc_setAssociatedObject(thirdPartyObject, &kSelectorKey, NSStringFromSelector(selector), OBJC_ASSOCIATION_RETAIN); 目前我正在使用此代码: objc_setAssociatedObject(thirdPartyObject, &kSelectorKey, NSStringFromSelector(selector), OBJC_ASSOCIATION_RETAIN);

Conversion to NSString works, but in feels wrong. 转换为NSString有效,但感觉不对。 There must be more appropriate solution. 必须有更合适的解决方案。

OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

The 3rd parameter needs to be an object type. 第3个参数需要是对象类型。 So, yes, you need that conversion. 所以,是的,你需要转换。

Try This. 尝试这个。
In my example i have passed indexpath you have to replace with your para meter 在我的例子中,我已经通过indexpath你必须用你的参数表替换

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCustomeCell *aCell;
    NSString *aStrIdentifier = @"yourIdentiFier";
    aCell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:aStrIdentifier];

    //you have to set your indexpath
    objc_setAssociatedObject(aCell.btnUpload_or_Add, @"objBtn", indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [aCell.YourButton addTarget:self action:@selector(yourButtonActiontapped:) forControlEvents:UIControlEventTouchUpInside];

    return aCell;
}

-(IBAction)yourButtonActiontapped:(UIButton *)sender{

    NSIndexPath *aIndPath =  objc_getAssociatedObject(sender, @"objBtn");
    NSLog(@"row:%@",aIndPath.row);
}

also you have to import #import <objc/runtime.h> 你也必须导入#import <objc/runtime.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对类对象使用objc_setAssociatedObject是否正确? - Is it correct to use objc_setAssociatedObject for class object? 如何在对象中使用objc_setAssociatedObject / objc_getAssociatedObject? - How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object? 如何存储非id类型变量使用objc_getAssociatedObject / objc_setAssociatedObject? - How to store not id type variable use a objc_getAssociatedObject/objc_setAssociatedObject? 使用objc_setAssociatedObject为UITableViewCell传递UIButton @selector参数 - using objc_setAssociatedObject to pass UIButton @selector argument for UITableViewCell objc_setAssociatedObject在iOS中不可用吗? - Is objc_setAssociatedObject not available in iOS? 理解 objc_setAssociatedObject 中的 UnsafeRawPointer - Understanding UnsafeRawPointer in objc_setAssociatedObject 在objc_SetAssociatedObject中使用非id指针作为值是否可以? - Is it ok to use a non-id pointer as value in objc_SetAssociatedObject? 如何释放通过objc_setAssociatedObject关联的对象? - How to release objects associated via objc_setAssociatedObject? 所有子类的类别集中的objc_setAssociatedObject - objc_setAssociatedObject in a category sets for all subclasses 在弱引用中使用 objc_setAssociatedObject - Using objc_setAssociatedObject with weak references
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM