简体   繁体   中英

get notified when UITextField becomeFirstResponder

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.

您将需要成为文本字段的委托并实现此可选委托方法:

- (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM