Detecting a change to text in UITextfield
Take advantage of the UITextFieldTextDidChange
notification or set a delegate on the text field and watch for textField:shouldChangeCharactersInRange:replacementString
.
If you want to watch for changes with a notification, you'll need something like this in your code to register for the notification
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:theTextField];
Here theTextField is the instance of UITextField
that you want to watch. The class of which self is an instance in the code above must then implement textFieldDidChange
, like so:
- (void)textFieldDidChange:(NSNotification *)notification {
// Do whatever you like to respond to text changes here.
}
If the text field is going to outlive the observer
, then you must deregister for notifications in the observer's dealloc
method. Actually it's a good idea to do this even if the text field does not outlive the observer.
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Other dealloc work
}
Swift 3.0
Process 1
Create IBOutlet of UITextfiled and Add Target to text field.
m_lblTxt.addTarget(self, action: #selector(self.textFieldDidChange), for: UIControlEvents.editingChanged)
func textFieldDidChange(textField:UITextField)
{
NSLog(textField.text!)
}
Process 2
m_lblTxt.delegate = self
//MARK: - TextField Delegates
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
print(textField.text!)
return true
}
Instead of observing notifications or implementing textField:shouldChangeCharacterInRange:replacementString:
, it's easier to just add an event target:
[textField addTarget:self
action:@selector(myTextFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
- (void)myTextFieldDidChange:(id)sender {
// Handle change.
}
Note that the event is UIControlEventEditingChanged
and not UIControlEventValueChanged
!
The advantages over the other two suggested solutions are:
- You don't need to remember to unregister your controller with the
NSNotificationCenter
. - The event handler is called after the change has been made which means
textField.text
contains the text the user actually entered. ThetextField:shouldChangeCharacterInRange:replacementString:
delegate method is called before the changes have been applied, sotextField.text
does not yet give you the text the user just entered – you'd have to apply the change yourself first.
For that, first you need to have your textfield have it delegate reference assigned. And the delgate, should preferably be, the vew controller which is the files owner of the view. Which goes like
myTextField.delegate = myViewControllerReferenceVariable
And in your viewController interface, tell you will be implementing UITextFieldDelegate by
@interface MyViewController:UIViewController<UITextFieldDelegate>
And in your view controller implementation override
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
So the code will look like
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
text = [textfield.text stringByReplacingCharactersInRange:range withString:string];
if (textfield == refToTextFieldYouWantToCheck) {
if ( ! [textToCheck isEqualToString:text] ) {
[theButtonRef setEnabled:YES];
}
}
return YES; //If you don't your textfield won't get any text in it
}
You can also subscribe to notification which is sort of messy IMHO You can find how to do it here.