简体   繁体   中英

(iOS) How do I get notified when the keyboard's back button is pressed?

I'm trying to determine when the keyboard's back button is pressed. When there is text in the text field, this is easy. I simply use the delegate method - (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string. The problem is when the text field is empty and the back button is pressed, this method isn't called.

Does anyone know how to get notified even when the text field is empty?

Could you default in a single space to the empty field when you begin editing (and the field is empty) so that the backspace becomes a normal event? You could remove the space when leaving the field and/or the first normal character is typed. Bit of a hack but I don't think you'll get to it any other way.

Check out this answer..

Receive iPhone keyboard events

In your keyPressed function, check to see if the textfield value has any content;

In the delegate method:

(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string

keep the earlier string and compare it with the string, if count of string is 1 less than the previous modified string it means delete is pressed. Otherwise store string into earlier string. hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM