如何传递一个string作为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:

 [NSString stringWithFormat:@"%i", ((UIControl*)sender).tag]; 

或者,如果您真的需要一个string作为UI对象的标识符,只需将其子类化并添加如下属性:

 @property (nonatomic, strong) NSString *stringID; 

然后使用它来代替使用标签属性。

你可以在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); } 

标签属性将只接受整数,如果黑整数值以获得一个string不是你的选项,你可以对UIButton进行分类,然后使用IB中的用户定义的运行时属性部分来设置在StoryBoard中的属性。

用一个新的非整数属性子类化UIButton:

 @interface MyCustomUIButton : UIButton @property (strong) NSString* stringTag; @end 

将你的button的类设置为子类:

身份检查器>自定义类>类=> MyCustomUIButton

从IB或通过代码设置属性值:

从IB:

身份检查器>用户定义的运行属性:

键path=> stringTag

Type => String

值=> yourstringvalue

或从代码:

 ((MyCustomUIButton*)yourButton).stringTag = @"yourstringvalue"; 

然后你可以从你的button操作方法中检索属性的值:

 -(void)yourMethod:(id)sender { NSLog(@"The string value is: ",((MyCustomUIButton*)sender).stringTag); // or use this idiom, to avoid a possible crash if it's the wrong class: if ( [sender respondsToSelector:@selector(stringTag)] ) testeee = [sender valueForKey:@"stringTag"]; else NSLog(@"this IS NOT a MyCustomUIButton, it's a plain UIButton"); // testeee will be NULL if you FORGOT to set the // User Defined Runtime Attribute for 'stringTag' in storyboard // note that a useful alternative to // if ( [sender respondsToSelector:@selector(stringTag)] ) // is... // if ( [sender respondsToSelector:NSSelectorFromString(@"stringTag")] ) } 

NSLog(@“部件号是:%@”,[NSString stringWithFormat:@“%d”,(UIControl *)sender).tag]);

据我所知标签总是键入int。

你需要手动将其转换为:

 NSString *temp : [[NSString alloc]init]; if(sender.tag == x){ temp = @"some string"; ) 

并为不同的标签制作案例。

请纠正我,如果我在这里错了:)

尝试

 -(void)pickTheQuiz:(id)sender{ NSNumber *tag = [NSNumber numberWithInt:sender.tag]; NSLog(@"The part number is:%@",[tag stringValue]); } 

有一个简单的方法用uibutton传递一个NSString参数,也就是设置buuton的标题的颜色和字体大小为0.0f.这是有效的,如果你使用一个图像([button setBackgroundImage:anyImage。forState:UIControlStateNormal];)on button而不是显示titl,然后你可以得到“sender.currentTitle”并使用这个variables