简体   繁体   English

当UITextField成为FirstResponder时得到通知

[英]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.

相关问题 UITextField成为FirstResponder和重叠子视图 - UITextField becomeFirstResponder and overlap subviews 尝试在编辑时使带有UITextField的自定义UITableViewCell成为FirstResponder - Trying to make a Custom UITableViewCell with a UITextField becomeFirstResponder when Editing UITextField成为firstResponder仅工作一次 - UITextField becomeFirstResponder works only once UITextField成为FirstResponder,但键盘未显示 - UITextField becomeFirstResponder but the keyboard doesn't showed MFMailComposeViewController:视图显示时如何获得通知? - MFMailComposeViewController: how to get notified when view appears? UIView - 如何在加载视图时收到通知? - UIView - How to get notified when the view is loaded? 如果嵌入到UINavigationController中,则在调用成为firstResponder并设置文本时,UITextField中缺少文本 - Missing text in UITextField while calling becomeFirstResponder and setting text, if embedded in UINavigationController UITextField如何调用viewDidAppear中的成为firstResponder来减慢当前动画的播放速度? - How come UITextField calling becomeFirstResponder in viewDidAppear slows down the present animation? 为什么UITextField成为firstResponder在我的子viewcontroller中不显示键盘? - why UITextField becomeFirstResponder doesn't show keyboard in my child viewcontroller? 单击按钮时如何获取 UITextField 值 - How to get UITextField values when button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM