Ankit Sachan
Ankit Sachan

Reputation: 7850

implement touchesBegan for UITextField

Can you catch TextField events when a Touch happens? So far I didn't see the Touch event get registered to TextField. For example,

    * (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
UITouch *touch = [event allTouches] anyObject;

if (touch view == TextFieldView) {
do somthing.....
}

This a dummy code what I want to implement is I want a method that get fired when user touches the text field for some period. If yes than how???

Enlighten me on this.

Thnx in advance.

Upvotes: 1

Views: 5567

Answers (3)

TechZen
TechZen

Reputation: 64428

I just tested this by writing a subclass of UITextField subclass that captures touches.

@interface TouchTestTextField : UITextField {}
@end

#import "TouchTestTextField.h"
@implementation TouchTestTextField

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan");
    NSLog(@"touches=%@,event=%@",touches,event);
}

@end

It easily captures touches and shows the text field as the view of the touch.

Upvotes: 2

vaddieg
vaddieg

Reputation: 626

Right-click on UITextField in the IB and link Touch Down event to required IBAction

Upvotes: 5

martinr
martinr

Reputation: 3812

You could try overriding [parentView hitTest:withEvent:] in the parent view and claim handling of touches for the textField on a per-touch basis when the app / UI is in the appropriate state.

I think all the touchesMoved/Cancelled/Ended events for that touch then go to the parent and not the textfield until the next touch. But test it and see.

EDIT

Or simply setting userInteractionEnabled=NO when you want the parent to handle touches instead of the textfield and setting it to =YES when you want the TextField to handle touches. See the Event Handling section of the iPhone Application Programming Guide.

Upvotes: 1

Related Questions