如何在iOS 8中检测UITextField上的删除键? [英] How to detect delete key on an UITextField in iOS 8?

查看:875
本文介绍了如何在iOS 8中检测UITextField上的删除键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将UITextField子类化,并实现了UIKeyInput协议的deleteBackward方法来检测被按下的退格。这在iOS 7上工作正常,但在iOS 8上没有。

I have subclassed UITextField and implemented the UIKeyInput protocol's deleteBackward method to detect backspace being pressed. This works fine on iOS 7 but not on iOS 8.

当我按退格键时,不再在UITextField上调用deleteBackward。

deleteBackward is not called on the UITextField anymore when I press the backspace key.

我已经检查了文档和发行说明,没有指出可能发生这种情况的原因。任何指针?

I've checked the documentation and the release notes and nothing points to the reason why this could happen. Any pointers?

推荐答案

你必须看一个 MBContactPicker 。通过我测试的iOS8上的Backspace按钮删除MBContactPicker上的联系人。它非常有效!您可以使用它作为示例。

You must look an example for MBContactPicker on github. Deletion of contacts at MBContactPicker via Backspace button on iOS8 tested by me. And it works greatly! You can use its as example.

MBContactPicker的作者使用next方法:当UITextField必须为空时(或者在它为空时调用becomeFirstResponder之前),他保存单个空白符号那里。然后当你按Backspace按钮时(当焦点设置为你的UITextField的文本结束时),方法

Author of MBContactPicker use next method: When UITextField must become empty (or before call becomeFirstResponder when it is empty), he save single whitespace symbol there. And then when you press Backspace button (when focus was set to end of text of your UITextField), method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

将工作。在其中你必须使用这样的检查:

will work. Inside it you must use check like this:

NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL isPressedBackspaceAfterSingleSpaceSymbol = [string isEqualToString:@""] && [resultString isEqualToString:@""] && range.location == 0 && range.length == 1;
if (isPressedBackspaceAfterSingleSpaceSymbol) {
    //  your actions for deleteBackward actions
}

因此,您必须始终控制UITextField包含单个空格。

So, you must always control that UITextField contains single whitespace.

这不是黑客攻击。因此,用户不会注意到某些行为已被更改

This is not hack. So, user willn't noticed about some behaviour was changed

这篇关于如何在iOS 8中检测UITextField上的删除键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆