How do I detect if a text label is pressed in iOS?
I'm new to iOS development and am creating a simple application. I have a custom numeric keypad with a two text labels. When you press any number on the keypad, the number will simply display in the first text label. Now what I need is if the user presses on the second te开发者_如何学Cxt label the keypad will automatically switch to that particular text label. I'm not quite sure how to go about doing this. Any help would be greatly appreciated!
You should be using UITextField
instead of UILabel
, then this would be handed automatically for you. Also, enabling the return key "next" on the keyboard will make a better user experience.
By using UITapGestureRecognizer, we can detect the user interaction with UILabel. Refer the following code,
label1.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(**myLabelOnClickAction:label1Name**)] autorelease];
[lablel1 addGestureRecognizer:tapGesture];
label2.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture1 =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(**myLabelOnClickAction:label2Name**)] autorelease];
[label2 addGestureRecognizer:tapGesture1];
By passing your label name in methodForLabeOnClickAction method, you can find which label you have clicked.
Hope this will help!
精彩评论