Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a string as a tag of UIButton

The tag value is an Integer:

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

In the receiver method:

-(void)pickTheQuiz:(id)sender{           
    NSLog(@"The part number is:%i",((UIControl*)sender).tag);//i need to receive a string value instead of numeric value
}
like image 560
Luca Avatar asked Mar 09 '12 09:03

Luca


1 Answers

You can convert the integer value of a tag to a NSString with:

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

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;

And then use it instead of use the tag property.

like image 170
bontoJR Avatar answered Sep 30 '22 18:09

bontoJR