Is there a touch method for UILabel?

Matoe picture Matoe · Jun 12, 2011 · Viewed 21.8k times · Source

I'd like to do an action if someone touches a predeclared UILabel, something like:

if (label is touched) {
    my actions;
}

Is there a method/way to do that?

Answer

Wilbur Vandrsmith picture Wilbur Vandrsmith · Jun 12, 2011

You could use a gesture recognizer:

- (void)someSetupMethod {
    // ...
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = \
    [[UITapGestureRecognizer alloc]
     initWithTarget:self action:@selector(didTapLabelWithGesture:)];
    [label addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
    // ...
}