如何将字符串作为UIButton的标记传递 [英] How to pass a string as a tag of UIButton
本文介绍了如何将字符串作为UIButton的标记传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
标记值为整数:
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
在接收方法中:
-(void)pickTheQuiz:(id)sender{
NSLog(@"The part number is:%i",((UIControl*)sender).tag);//i need to receive a string value instead of numeric value
}
推荐答案
您可以将标签的整数值转换为NSString:
You can convert the integer value of a tag to a NSString with:
[NSString stringWithFormat:@"%i", ((UIControl*)sender).tag];
或者,如果你真的需要一个字符串作为UI对象的标识符,只需将其子类化并添加一个属性如:
Or, if you really need a string as identifier for an UI object, just subclass it and add a property like:
@property (nonatomic, strong) NSString *stringID;
然后使用它而不是使用tag属性。
And then use it instead of use the tag property.
这篇关于如何将字符串作为UIButton的标记传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文