Search code examples
objective-cioscocoa-touchuilabeltouch-event

Is there a touch method for UILabel?


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?


Solution

  • 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 {
        // ...
    }