[英]get notified when UITextField becomeFirstResponder
How I can get notified when UITextField
becomeFirstResponder
? 当
UITextField
becomeFirstResponder
时如何获得通知?
I can check like isFirstResponder
or set to to become first Responder by becomeFirstResponder
我可以像
isFirstResponder
一样检查,也可以设置为通过成为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. 除了实现
UITextFieldDelegate
方法textFieldDidBeginEditing:
,您还可以注册UITextFieldTextDidBeginEditingNotification
通知。
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.
不幸的是,在我的情况下,我不能使用它,因为控制器是我的UITextField子类的委托,并且我需要修改它的占位符文本。 Instead I override the methods
- (BOOL)canBecomeFirstResponder;
相反,我重写了方法
- (BOOL)canBecomeFirstResponder;
& - (BOOL)resignFirstResponder;
&
- (BOOL)resignFirstResponder;
. 。 Be sure to call super.
一定要叫超级。
For OSX you should be aware of buttons having side effects when relying on the NSControlTextDidBeginEditingNotification
. 对于OSX,在依赖
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. 当您开始编辑时,按一些按钮,然后继续编辑文本字段,当您继续编辑时,不会再收到
NSControlTextDidBeginEditingNotification
。
In case this behavior causes a problem, UITextFieldTextDidChangeNotification
might be an alternative. 如果此行为引起问题,则可以选择
UITextFieldTextDidChangeNotification
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.