且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iOS 8中检测UITextField上的删除键?

更新时间:2023-11-17 14:33:34

你必须看一个 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