I have three UILabels. I want to detect which label is Tapped, and then retrieve the string value of that label. this is how I am trying, I could only manage to detect tapped position, But I couldn't detect which label was tapped.
Label Creation
for (NSInteger i=1; i<=[pdfs count]; i++){
UILabel *newLabel=[[UILabel alloc] init];
newLabel.text = [NSString stringWithFormat:[[pdfs objectAtIndex:(i-1)] lastPathComponent]];
newLabel.frame = CGRectMake(10, 60*i, 320, 20);
newLabel.tag=i;
newLabel.font = [UIFont systemFontOfSize:20.0f];
newLabel.backgroundColor = [UIColor clearColor];
newLabel.userInteractionEnabled = YES;
[self.view addSubview:newLabel];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[newLabel addGestureRecognizer:singleTap];
[newLabel release], newLabel=nil;
[singleTap release];
}
Detect Taps
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
CGPoint location;
location = [recognizer locationInView:self.view];
NSString *documentName;
if(location.y<150.0){
documentName = [[pdfs objectAtIndex:0] lastPathComponent];
}
else{
documentName = [[pdfs objectAtIndex:1] lastPathComponent];
}
UIGestureRecognizer has a reference to a view it is attached to, so you can get your label's tag from it:
int touchedtag = recognizer.view.tag;
documentName = [[pdfs objectAtIndex:touchedtag-1] lastPathComponent];