Detect backspace in empty UITextField

2019-01-01 05:16发布

Is there any way to detect when the Backspace/Delete key is pressed in the iPhone keyboard on a UITextField that is empty? I want to know when Backspace is pressed only if the UITextField is empty.


Based on the suggestion from @Alex Reynolds in a comment, I've added the following code while creating my text field:

[[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(handleTextFieldChanged:)
              name:UITextFieldTextDidChangeNotification
            object:searchTextField];

This notification is received (handleTextFieldChanged function is called), but still not when I press the Backspace key in an empty field. Any ideas?


There seems to be some confusion around this question. I want to receive a notification when the Backspace key is pressed. That's it. But the solution must also work when the UITextField is already empty.

23条回答
余欢
2楼-- · 2019-01-01 05:32

The best use that I have found for detecting backspace is detecting when the user has pressed backspace in an empty UITextField. For example, if you have 'bubbled' recipients in the mail app, when you hit backspace in the UITextField, it selects the last 'bubbled' recipient.

This can be done in a similar way to Jacob Caraballo's answer. But in Jacob's answer, if the UITextField has one character left when you hit backspace, by the time the delegate message is received, the UITextField will already be empty, so you're effectively detecting backspace on a text field with at most one characters.

Actually, if you want to detect backspace on a UITextField with exactly zero characters (empty), then you should send the message to the delegate before the call to super deleteBackward. For example:

#import "MyTextField.h"

//Text field that detects when backspace is hit with empty text
@implementation MyTextField

#pragma mark - UIKeyInput protocol
-(void)deleteBackward
{
  BOOL isTextFieldEmpty = (self.text.length == 0);
  if (isTextFieldEmpty) {
    if ([self.delegate 
         respondsToSelector:@selector(textFieldDidHitBackspaceWithEmptyText:)]) {

        [self.delegate textFieldDidHitBackspaceWithEmptyText:self];
        }
    }
    [super deleteBackward];
}
@end

The interface for such a text field would look something like this:

@protocol MyTextFieldDelegate;

@interface MyTextField : UITextField
@property(nonatomic, weak) id<MyTextFieldDelegate> delegate;
@end

@protocol MyTextFieldDelegate <UITextFieldDelegate>
@optional
-(void)textFieldDidHitBackspaceWithEmptyText:(MyTextField *)textField;
@end
查看更多
无色无味的生活
3楼-- · 2019-01-01 05:33

I've founded other way easier than subclass solution. Even its little bit strange but it works ok.

- (BOOL)textView:(UITextView *)textView 
        shouldChangeTextInRange:(NSRange)range 
        replacementText:(NSString *)text
{
    const char * _char = [text cStringUsingEncoding:NSUTF8StringEncoding];
    int isBackSpace = strcmp(_char, "\b");

    if (isBackSpace == -8) {
       // is backspace
    }

    return YES;
}

It's a little bit strange for result of compare is -8. Maybe I'll wrong in some point of C Programming. But its right work ;)

查看更多
孤独总比滥情好
4楼-- · 2019-01-01 05:34

For the ones who has problems about the Jacob's answer I implemented my textfield subclass as following and it works great!

#import <UIKit/UIKit.h>

@class HTTextField;

@protocol HTBackspaceDelegate <NSObject>

@optional
- (void)textFieldDidBackspace:(HTTextField*)textField;
@end

@interface HTTextField : UITextField<UIKeyInput>

@property (nonatomic, assign) id<HTBackspaceDelegate> backspaceDelegate;

@end


#import "HTTextField.h"

@implementation HTTextField

- (void)deleteBackward {
    [super deleteBackward];
    if ([self.backspaceDelegate respondsToSelector:@selector(textFieldDidBackspace:)]){
        [self.backspaceDelegate textFieldDidBackspace:self];
    }
}

- (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);
        }
    }

    if (![textField.text length] && [[[UIDevice currentDevice] systemVersion] intValue] >= 8) {
        [self deleteBackward];
    }

    return shouldDelete;
}

@end
查看更多
余欢
5楼-- · 2019-01-01 05:34

In iOS 6, the deleteBackward method is called on the UITextField when backspace is pressed, including when the field is empty. So you can subclass UITextField and provide your own deleteBackward implementation (invoking super's as well.)

I'm still supporting iOS 5 though so I'll need a combination of Andrew's answer and this.

查看更多
初与友歌
6楼-- · 2019-01-01 05:37

This may be a long shot but it could work. Try setting the text field's text to a zero width space character \u200B. When backspace is pressed on a text field that appears empty, it will actually delete your space. Then you can just reinsert the space.

May not work if the user manages to move the caret to the left of the space.

查看更多
路过你的时光
7楼-- · 2019-01-01 05:40

please use below code it will help you to detect keyboard delete key even if you textfield is empty.

Objective C :

- (BOOL)keyboardInputShouldDelete:(UITextField *)textField { return YES; }

Swift :

func keyboardInputShouldDelete(_ textField: UITextField) -> Bool { return true }
查看更多
登录 后发表回答