views:

381

answers:

3

Hello,

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.

A: 

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.

martinr
A: 

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

vaddieg
what method will be called when the user remove touch from textfield
Ankit Sachan
+1  A: 

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.

TechZen
I accept that but the situation here is that I have a textfields inside scrollview
Ankit Sachan
Scrollviews are a problem. They trap touches to decide if they should scroll or not so they don't pass touches to subviews as other views do. I think the problem has been addressed on STO before. You should search on scrollview touches.
TechZen