Home » Ios » get notified when UITextField becomeFirstResponder

get notified when UITextField becomeFirstResponder

Posted by: admin February 24, 2018 Leave a comment

Questions:

How I can get notified when UITextField becomeFirstResponder ?

I can check like isFirstResponder or set to to become first Responder by becomeFirstResponder

I want to get notified or handle an event when a user make this text field first responder.

Thanks.

Answers:

You will need to be come the text field’s delegate and implement this optional delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField; 

Questions:
Answers:

Besides implementing the UITextFieldDelegate method textFieldDidBeginEditing:, you can register for the UITextFieldTextDidBeginEditingNotification notification.

The notification method could be:

- (void)someTextFieldDidBeginEditing:(NSNotification *)notification {
    UITextField *textField = (UITextField *)notification.object;
}

Questions:
Answers:

The delegate is definitely the way to go. Unfortunately, in my situation I couldn’t use that, because the controller was the delegate of my UITextField subclass and I needed to modify it’s placeholder text. Instead I override the methods - (BOOL)canBecomeFirstResponder; & - (BOOL)resignFirstResponder;. Be sure to call super.

Questions:
Answers:

For OSX you should be aware of buttons having side effects when relying on the NSControlTextDidBeginEditingNotification.

When you start editing, push some buttons, and resume editing the text field, you won’t get a second NSControlTextDidBeginEditingNotification when you resume editing.

In case this behavior causes a problem, UITextFieldTextDidChangeNotification might be an alternative.