标记值是整数:
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:addressField forState:UIControlStateNormal];
[button addTarget:self action:@selector(pickTheQuiz:) forControlEvents:UIControlEventTouchUpInside];
button.tag=1;//or what other integer value, i need to put a string value
Run Code Online (Sandbox Code Playgroud)
在接收方法中:
-(void)pickTheQuiz:(id)sender{
NSLog(@"The part number is:%i",((UIControl*)sender).tag);//i need to receive a string value instead of numeric value
}
Run Code Online (Sandbox Code Playgroud)
bon*_*oJR 11
您可以将标记的整数值转换为NSString:
[NSString stringWithFormat:@"%i", ((UIControl*)sender).tag];
Run Code Online (Sandbox Code Playgroud)
或者,如果您确实需要一个字符串作为UI对象的标识符,只需将其子类化并添加如下属性:
@property (nonatomic, strong) NSString *stringID;
Run Code Online (Sandbox Code Playgroud)
然后使用它而不是使用tag属性.
你可以在功能objc_runtime的帮助下做到这一点
#import <objc/runtime.h>
static char kButtonAssociatedKey;
NSString *aStrKey = [NSString stringWithFormat:@"Any Key"];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:addressField forState:UIControlStateNormal];
[button addTarget:self action:@selector(pickTheQuiz:) forControlEvents:UIControlEventTouchUpInside];
button.tag=1;
objc_setAssociatedObject(button,
&kButtonAssociatedKey,
aStrKey,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
-(void)pickTheQuiz:(id)sender
{
NSString *aStrKey = objc_getAssociatedObject(sender, &kButtonAssociatedKey);
objc_removeAssociatedObjects(sender);
NSLog(@"%@", aStrKey);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13976 次 |
| 最近记录: |