문제

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)

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top