Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically perform an action on UILabel touchUpInside?

Tags:

uilabel

iphone

I have a bunch of UILabels that I add through code and I want to perform a specific action for each if the user's finger touches up inside (much like UIButton's touchUpInside in IB). What is the best way of doing this?

like image 704
Don Wilson Avatar asked Nov 28 '22 19:11

Don Wilson


2 Answers

The easiest way is probably to use UIButton instead of UILabel. A custom UIButton has no border and can look like a plain label, but handles the event tracking for you.

Otherwise, you must derive from UILabel and implement the UIResponder calls for touch handling.

like image 76
drawnonward Avatar answered Dec 07 '22 00:12

drawnonward


You can use UITapGestureRecognizer to perform action over the label Fields.

Set the number of taps and touches on the UITapGestureRecognizer object.

[oneFingerTwoTaps setNumberOfTapsRequired:1];
[oneFingerTwoTaps setNumberOfTouchesRequired:1]; 

set user interaction of label field like

labelField.userInteractionEnabled = YES;

then add target method over the

UITapGestureRecognizer object.
like image 44
Pathetic Learner Avatar answered Dec 06 '22 23:12

Pathetic Learner