Detect backspace in empty UITextField
Update: See JacobCaraballo's answer for an example that overrides -[UITextField deleteBackward]
.
Check out UITextInput
, specifically UIKeyInput
has a deleteBackward
delegate method that always gets called when the delete key is pressed. If you're doing something simple, then you might consider just subclassing UILabel
and making it conform to the UIKeyInput
protocol, as done by SimpleTextInput and this iPhone UIKeyInput Example. Note: UITextInput
and its relatives (including UIKeyInput
) are only available in iOS 3.2 and later.
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.
Code like following:
@interface MyTextField : UITextField
@end
@implementation MyTextField
- (void)deleteBackward
{
[super deleteBackward];
//At here, you can handle backspace key pressed event even the text field is empty
}
@end
At last, do forget to change the Custom Class property of the Text Field to "MyTextField"
Swift 4:
Subclass UITextField
:
// MyTextField.swift
import UIKit
protocol MyTextFieldDelegate: AnyObject {
func textFieldDidDelete()
}
class MyTextField: UITextField {
weak var myDelegate: MyTextFieldDelegate?
override func deleteBackward() {
super.deleteBackward()
myDelegate?.textFieldDidDelete()
}
}
Implementation:
// ViewController.swift
import UIKit
class ViewController: UIViewController, MyTextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// initialize textField
let input = MyTextField(frame: CGRect(x: 50, y: 50, width: 150, height: 40))
// set viewController as "myDelegate"
input.myDelegate = self
// add textField to view
view.addSubview(input)
// focus the text field
input.becomeFirstResponder()
}
func textFieldDidDelete() {
print("delete")
}
}
Objective-C:
Subclass UITextField
:
//Header
//MyTextField.h
//create delegate protocol
@protocol MyTextFieldDelegate <NSObject>
@optional
- (void)textFieldDidDelete;
@end
@interface MyTextField : UITextField<UIKeyInput>
//create "myDelegate"
@property (nonatomic, assign) id<MyTextFieldDelegate> myDelegate;
@end
//Implementation
#import "MyTextField.h"
@implementation MyTextField
- (void)deleteBackward {
[super deleteBackward];
if ([_myDelegate respondsToSelector:@selector(textFieldDidDelete)]){
[_myDelegate textFieldDidDelete];
}
}
@end
Now simply add MyTextFieldDelegate to your UIViewController
and set your UITextFields
myDelegate to self
:
//View Controller Header
#import "MyTextField.h"
//add "MyTextFieldDelegate" to you view controller
@interface ViewController : UIViewController <MyTextFieldDelegate>
@end
//View Controller Implementation
- (void)viewDidLoad {
//initialize your text field
MyTextField *input =
[[MyTextField alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
//set your view controller as "myDelegate"
input.myDelegate = self;
//add your text field to the view
[self.view addSubview:input];
}
//MyTextField Delegate
- (void)textFieldDidDelete {
NSLog(@"delete");
}