Alex Rivera | Logout

How can I respond to touch events on a UITextField?

Asked 2012-01-10T02:06:13.603
17

I want to respond to touch events (specifically UIControlEventTouchUpInside and UIControlEventTouchUpOutside) on a UITextField, but the only event that comes through is UIControlEventTouchCancel.

I can't use UIControlEventEditingDidBegin, because it only occurs when field gains focus, which is not what I'm trying to detect. I want to know when the user lifts their finger, and whether it was inside or outside the text field, regardless of current focus state.

Does anyone know a way to accomplish this?

To be clear I've tried using addTarget:action:forControlEvents and it does not work. The touch up event is prevented by the cancel. I want a way to detect the touch up before the UITextField consumes it and produces the Cancel event.

Also, I'd like to avoid creating any extra views if possible.

Edit
Report

2 Answers

4

Well a round about way of doing it would be to create a UIButton with the fill set to clear to cover all of the space, and a UITextField on top of it. If the user touches in the text field, the text field should send an event, and if the user touches outside of the text field, the UIButton will send the event.

answered 2012-01-10T02:12:12.113
0

Add a target for your desired event programatically, or, use IB.

UITextField * touchyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 21.0)];

[touchyTextField addTarget:self 
                    action:@selector(yourDesiredMethod) 
          forControlEvents:UIControlEventTouchUpInside];
answered 2012-01-10T02:11:31.240

Your Answer