0

Men, I don't believe that this will be a problem to me. If I press a key in keyboard, the UITextField delegate trigger the function:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       if ([string lenght]==0) NSLog("Backspace was pressed");
 }

But the problem is: when the text is empty and I press backspace, this function IS NOT called. There are a way to detect the backspace was pressed in this situation?

Or I will have to send this bug to Apple?

Ps. I need this for the cursor go to previous UITextFild (if have one character and press backspace, it work)

Tad Donaghe
  • 6,625
  • 1
  • 29
  • 64
Rodrigo
  • 11,909
  • 23
  • 68
  • 101

2 Answers2

1

Well I don't think this is a bug because the textfield is empty and therefore no characters have been changed in the range hence why the method isn't being fired. The UITextFieldDelegate Documentation by Apple says:

The text field calls this method whenever the user types a new character in the text field or deletes an existing character.

There are no existing characters in the TextField so the method is not fired. It doesn't help answer the question but it's not a bug in the SDK

To get the behaviour you want, this question is already answered here: Can I detect the delete key even if the UITextField is empty?

Community
  • 1
  • 1
Suhail Patel
  • 13,644
  • 2
  • 44
  • 49
  • I try to find the answer to this question in the SO and don't find it :( Tanks, I will try to solve it. Lol, it use "delete" key and I only search for "backspace" key... I will try increase my skill in my search. :) – Rodrigo Aug 26 '11 at 14:22
  • You could register for the UITextFieldTextDidChangeNotification notification which gets fired in this scenario and go to the previous textfield? (Credit: http://www.iphonedevsdk.com/forum/57297-post8.html) – Suhail Patel Aug 26 '11 at 14:26
  • This not work, the notification do not start in this situation. I will use the permanent "white space". – Rodrigo Aug 26 '11 at 14:43
  • It may have changed between SDK's (I haven't tried it personally). I think the Permanent Whitespace may be the best alternative to get the behaviour you want. – Suhail Patel Aug 26 '11 at 14:48
  • Only to finish. I didn't find solution to this. I will have to live with this. – Rodrigo Aug 29 '11 at 13:12
0

Actually this is not a bug. Here the delegate methods are not catching the event as there is no change in any values of textfield.

This can be implemented by subclassing UITextField and adding it as custom class of desired textfield. Then override the method "deleteBackward" This method will catch all the backspace events.

Code in Swift:

override func deleteBackward() {
        super.deleteBackward()
        // Enter your stuff here
    }

Also make sure you that are calling super method too, as it is doing the basic functionalities of the event.

Ajith K Jose
  • 131
  • 1
  • 9