开发者

Solve Bug in UITextField: When it is empty, Pressing backspace doesn't Trigger correct function

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 pres开发者_开发百科sed");
 }

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)


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜