解决UITextField中的Bug:当它为空时,按退格键不会触发正确的功能
伙计们,我不相信这对我来说是个问题。如果我按下键盘上的某个键,UITextField 委托会触发该函数:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string lenght]==0) NSLog("Backspace was pressed");
}
但问题是:当文本为空并且我按退格键时,不会调用此函数。有没有办法检测在这种情况下按下了退格键?
或者我必须将此错误发送给Apple?
诗。我需要这个来让光标转到上一个 UITextFild (如果有一个字符并按退格键,它就可以工作)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我不认为这是一个错误,因为文本字段是空的,因此范围内没有更改任何字符,因此该方法没有被触发。 UITextFieldDelegate 文档苹果 说:
TextField 中不存在任何字符,因此不会触发该方法。它无助于回答问题,但它不是 SDK 中的错误
为了获得您想要的行为,这个问题已经在这里得到回答:即使 UITextField 为空,我也能检测到删除键吗?
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:
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?
实际上这不是一个错误。这里委托方法没有捕获事件,因为文本字段的任何值都没有变化。
这可以通过子类化 UITextField 并将其添加为所需文本字段的自定义类来实现。
然后重写方法“deleteBackward”
此方法将捕获所有退格事件。
Swift 中的代码:
还要确保您也调用了 super 方法,因为它正在执行事件的基本功能。
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:
Also make sure you that are calling super method too, as it is doing the basic functionalities of the event.