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

Discussion in 'iOS Programming' started by namanhams, Dec 12, 2009.

  1. macrumors regular

    Jun 3, 2009
    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 ?

  2. macrumors regular


    Jun 22, 2009
    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:
    //Txt will be an NSString called in your header file
    if ([txt length]>textfield.text){
    //Your action
    I'm not sure how accurate this is but it just might work.
  3. macrumors 604

    Jul 29, 2003
    Silicon Valley
    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.
  4. thread starter macrumors regular

    Jun 3, 2009
    @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".

Share This Page