[英]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.