简体   繁体   English

(iOS)按下键盘的后退按钮时如何获得通知?

[英](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. 我只使用委托方法-(BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replaceString:(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 接收iPhone键盘事件

In your keyPressed function, check to see if the textfield value has any content; 在keyPressed函数中,检查textfield值是否具有任何内容;

In the delegate method: 在委托方法中:

(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string (布尔)textView:(UITextView *)textView应该ChangeTextInRange:(NSRange)范围替换Text:(NSString *)字符串

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. 保留先前的字符串并将其与该字符串进行比较,如果字符串的计数比先前修改的字符串小1,则表示已按下删除键。 Otherwise store string into earlier string. 否则,将字符串存储到较早的字符串中。 hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下后退按钮时如何执行操作 - How do I perform an operation when the back button is pressed iOS-按下按钮时,如何让系统远程呼叫服务器? - iOS - when a button is pressed, how do I get the system to make a remote call to my server? 如何在按下按钮时更改按钮的颜色,并在按下其他按钮时重置为原始颜色? - How do I change a button's color when pressed and reset to original color when a different button is pressed? 如何在iOS的蓝牙键盘中检测何时按下播放按钮 - How can i detect when play button is pressed in a bluetooth keyboard in iOS 如何在按下按钮时使按钮弹出警报视图并返回主视图控制器? - How Do I Make A Button Get An Alert View To Pop Up When Pressed And Go Back To The Main View Controller? iOS-按下按钮时关闭键盘 - iOS - close keyboard when button is pressed 检测iOS键盘上何时按下“ @ 123”按钮 - Detect when the “@123” button is pressed on the iOS keyboard iPad键盘消失时如何获得通知? - How to get notified when the iPad keyboard disappears? iOS WatchKit - 如何检测Watch上何时按下后退按钮? - iOS WatchKit - How to detect when back button is pressed on Watch? 用户在iOS中启动消息传递或邮件发送应用程序时如何获得通知 - How do I get notified when user starts messaging or mailing app in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM