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进行了子类化,并实现了UIKeyInput协议的deletebackwards方法,以检测正在按下的backspace。这适用于iOS 7,但不适用于iOS 8。
deleteBackward is not called on the UITextField anymore when I press the backspace key.
当我按退格键时,UITextField不再调用deleteback。
I've checked the documentation and the release notes and nothing points to the reason why this could happen. Any pointers?
我已经检查了文档和发布说明,没有什么能说明为什么会发生这种情况。指针吗?
10
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.
您必须查看github上MBContactPicker的示例。通过我测试的iOS8上的Backspace按钮删除MBContactPicker上的联系人。和它的工作原理!你可以用它作为例子。
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
MBContactPicker的作者使用next方法:当UITextField必须为空时(或者在调用变成空时才调用它),他在那里保存一个空格符号。然后当您按下Backspace按钮(当焦点被设置为UITextField文本的结尾时)时,方法
- (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
}
So, you must always control that UITextField contains single whitespace.
因此,必须始终控制UITextField包含单个空格。
This is not hack. So, user willn't noticed about some behaviour was changed
这不是黑客。因此,用户不会注意到某些行为发生了改变
46
A lot of people have been saying this is a bug, but being that this problem still exists in the GM I'm starting to think it might be a change in logic. With that said, I wrote this bit of code for my app and have tested it on iOS 7-8.
很多人都说这是一个错误,但由于这个问题仍然存在于通用汽车,我开始认为这可能是逻辑上的改变。尽管如此,我还是为我的应用编写了这段代码,并在iOS 7-8上进行了测试。
Add the following method to your UITextField
subclass.
向UITextField子类添加以下方法。
- (BOOL)keyboardInputShouldDelete:(UITextField *)textField {
BOOL shouldDelete = YES;
if ([UITextField instancesRespondToSelector:_cmd]) {
BOOL (*keyboardInputShouldDelete)(id, SEL, UITextField *) = (BOOL (*)(id, SEL, UITextField *))[UITextField instanceMethodForSelector:_cmd];
if (keyboardInputShouldDelete) {
shouldDelete = keyboardInputShouldDelete(self, _cmd, textField);
}
}
BOOL isIos8 = ([[[UIDevice currentDevice] systemVersion] intValue] == 8);
BOOL isLessThanIos8_3 = ([[[UIDevice currentDevice] systemVersion] floatValue] <8.3f);
if (![textField.text length] && isIos8 && isLessThanIos8_3) {
[self deleteBackward];
}
return shouldDelete;
}
This code is slightly before the red line of private API's, however you should have no problem using it. My app with this code is in the app store.
此代码略早于私有API的红线,但是使用它应该没有问题。我的这个代码的应用在应用商店里。
To explain a little, were calling the super implementation of this method to avoid losing code. After were going to call -deleteBackward
if there is no text and the iOS version is between 8-8.2.
为了解释一下,我们调用了这个方法的超级实现,以避免丢失代码。如果没有文本,将调用- deleteback,而iOS版本在8-8.2之间。
EDIT: 1/22/15
编辑:1/22/15
It also might be helpful to subclass the -deleteBackward
method of your subclassed UITextField
. This fixes a few conditional bugs. One being if you use a custom keyboard. Heres an example of the method.
它还可能有助于子类化您的子类UITextField的- deleteback方法。这修复了一些有条件的bug。一个是使用自定义键盘。这是这个方法的一个例子。
- (void)deleteBackward {
BOOL shouldDismiss = [self.text length] == 0;
[super deleteBackward];
if (shouldDismiss) {
if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
[self.delegate textField:self shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];
}
}
}
EDIT: 4/13/15
编辑:4/13/15
As @Gee.E commented, iOS 8.3 has fixed this issue. The code has been updated to reflect the changes.
@Gee。E评论说,ios8.3已经解决了这个问题。代码已被更新以反映更改。
20
You can detect when user deletes text by using backspace by implementing UITextField delegate method:
通过实现UITextField委托方法,您可以使用backspace检测用户何时删除文本:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.length==1 && string.length==0)
NSLog(@"backspace tapped");
return YES;
}
5
Swift 2.2:
斯威夫特2.2:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "" {
print("Backspace has been pressed")
}
return true
}
4
In iOS8, some custom keyboards delete whole word, so only check string.length is OK.
在iOS8中,一些自定义键盘会删除整个单词,所以只检查字符串。长度是好的。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (string.length==0) { //Delete any cases
if(range.length > 1){
//Delete whole word
}
else if(range.length == 1){
//Delete single letter
}
else if(range.length == 0){
//Tap delete key when textField empty
}
}
return YES;
}
1
Swift 2.0 version for Detecting BackSpace based deletion, referencing code post from almas
Swift 2.0版本,用于检测基于空格的删除,引用来自almas的代码
//For Detecting Backspace based Deletion of Entire Word in TextField
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if (range.length == 1 && string.isEmpty){
print("Used Backspace")
}
return true
}
0
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
int isBackSpace = strcmp(_char, "\b");
if (isBackSpace == -8) {
NSLog(@"Backspace was pressed");
}
return YES;
}
Basically this method detects which button you are pressing (or have just pressed). This input comes in as an NSString. We convert this NSString to a C char type and then compare it to the traditional backspace character (\b). Then if this strcmp is equal to -8, we can detect it as a backspace.
基本上这个方法可以检测你按的是哪个按钮(或者刚刚按过)。这个输入以NSString的形式出现。我们将这个NSString转换为C char类型,然后将它与传统的backspace字符(\b)进行比较。然后,如果这个strcmp等于-8,我们可以将它作为一个后备空间进行检测。
0
swift 2:
斯威夫特2:
if (string.characters.count == 0 && range.length == 1) {
return true
}
you should use like this string.characters.count
你应该用这个字符串,字符,计数