tags:

views:

43

answers:

1

Hello friends,

I have a UILabel control in a view. I want to detect touch event occurred when this label touched. I added the following code, which should work whenever touch happens on the view.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

// This should be called only when the label is touched, not all the time..
[ [UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.mywebsite.com"] ];

}

Whenever that particular label is touched my code should do the process further, not when the touch is happening anywhere in the view. How do i find out the particular label (or) any control is touched under touchesEnded function ?

Could someone guide me on this?

Thank you.

+1  A: 

You can do this by performing a hitTest on the label:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:label];
    if([label hitTest:point withEvent:nil]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.mywebsite.com"]];
    } else {
        [super touchesEnded:touches withEvent:event];
    }
}
glorifiedHacker
Great! It worked. Thank you.