PDA

View Full Version : UITextField : Any way to detect the Delete key event when the field is empty ?




namanhams
Dec 12, 2009, 03:59 AM
I'm trying to do something similar to the Mail application on Iphone : when you add a recipient, if you hit the Delete key when the field is empty, then the cursor disappears and then bubble will be highlighted.

Any idea on how we can capture the Delete key event when the text field is empty ? Or if there's no way, then do you have any idea how the Mail application can work like that ?

Thanks.



drf1229
Dec 12, 2009, 07:50 PM
I'm trying to do something similar to the Mail application on Iphone : when you add a recipient, if you hit the Delete key when the field is empty, then the cursor disappears and then bubble will be highlighted.

Any idea on how we can capture the Delete key event when the text field is empty ? Or if there's no way, then do you have any idea how the Mail application can work like that ?

Thanks.

I'm not sure if this is possible through one action, however, I have a suggestion:
Make an IBAction (textfieldDidBeginEditing or w.e its called) and try something like this:
-(void)viewDidLoad{
//Txt will be an NSString called in your header file
txt=textfield.text;


}
-(IBAction)textFieldDidBeginEditing{
if ([txt length]>textfield.text){
//Your action

}
txt=textfield.text;
}
I'm not sure how accurate this is but it just might work.

firewood
Dec 12, 2009, 09:38 PM
Put a leading space character in the textfield. Delete it before you use it in the email header. Use the delegate method to allow each change to the textfield, but check the size each call. If the number of characters in the textfield ever goes to zero, you know the user has either hit the delete key, or cut the entire field.

namanhams
Dec 13, 2009, 12:26 AM
@drf1229 : i think it can not capture the event

@firewood : i did try this but it has some unexpected effect. If you tap the text field for a few times, you will see the cursor jumps back and forth between the current position and zero position (leftmost position). Then sometimes we can not capture the delete key event. I did go arround this by putting any non-space character and it works, but it's not what i want.

Also in the Mail application, when you tap the text field, it only pop up "Paste" button, that means the text field is really emtpy. If we put leading space character, it will aslo pop up "Select", "Select all".