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.

有帮助吗?

解决方案

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

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

其他提示

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

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.

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top