UITextField 失去焦点事件
我在 MyCustomUIView 类中有一个 UITextField,当 UITextField 失去焦点时,我想隐藏该字段并在适当的位置显示其他内容。
UITextField
的委托通过 IB 设置为 MyCustomUIView
,并且我还有指向 IBAction< 的“退出时结束”和“编辑结束”事件
MyCustomUIView
中的 /code> 方法。
@interface MyCustomUIView : UIView {
IBOutlet UITextField *myTextField;
}
-(IBAction)textFieldLostFocus:(UITextField *)textField;
@end
但是,当 UITextField 失去焦点时,这些事件似乎都不会被触发。 你如何捕获/寻找这个事件?
UITextField
的委托设置为 MyCustomUIView
,因此我收到 textFieldShouldReturn
消息以在完成后关闭键盘。
但我还感兴趣的是,当用户按下屏幕上的其他区域(例如另一个控件或空白区域)并且文本字段失去焦点时。
I have an UITextField in a MyCustomUIView class and when the UITextField loses focus, I'd like to hide the field and show something else in place.
The delegate for the UITextField
is set to MyCustomUIView
via IB and I also have 'Did End On Exit' and 'Editing Did End' events pointing to an IBAction
method within MyCustomUIView
.
@interface MyCustomUIView : UIView {
IBOutlet UITextField *myTextField;
}
-(IBAction)textFieldLostFocus:(UITextField *)textField;
@end
However, neither of these events seem to get fired when the UITextField loses focus. How do you trap/look for this event?
The delegate for the UITextField
is set as MyCustomUIView
so I am receiving textFieldShouldReturn
message to dismiss the keyboard when done.
But what I'm also interested in is figuring when the user presses some other area on the screen (say another control or just blank area) and the text field has lost focus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试使用委托执行以下方法:
这对我有用。
Try using delegate for the following method:
That worked for me.
我相信您需要将您的视图指定为 UITextField 委托,如下所示:
作为额外的好处,这就是当他们按下“完成”或返回按钮时键盘消失的方法,具体取决于您如何设置该属性:
I believe you need to designate your view as a UITextField delegate like so:
As an added bonus, this is how you get the keyboard to go away when they press the "done" or return buttons, depending on how you have set that property:
resignFirstResponder 解决方案的问题在于,它只能由显式的键盘或UITextField事件触发。
我还在寻找一个“失去焦点事件”来隐藏键盘,如果点击了文本字段之外的某个地方。
我遇到的唯一接近且实用的“解决方案”是,禁用其他视图的交互,直到用户完成编辑(点击键盘上的完成/返回),但仍然能够在文本字段之间跳转以进行更正,而无需每次都需要滑出和滑入键盘。
以下代码片段可能对想要做同样事情的人有用:
The problem with the resignFirstResponder solution solely is, that it can only be triggered by an explicit keyboard or UITextField event.
I was also looking for a "lost focus event" to hide the keyboard, if somewhere outside the textfield was tapped on.
The only close and practical "solution" I came across is, to disable interactions for other views until the user is finished with the editing (hitting done/return on the keyboard) but still to be able to jump between the textfields to make corrections without the need of sliding out and in the keyboard everytime.
The following snippet maybe useful to someone, who wants to do the same thing:
您可能必须对
UITextField
进行子类化并重写resignFirstResponder
。resignFirstResponder
将在文本字段失去焦点时被调用。You might have to subclass the
UITextField
and overrideresignFirstResponder
.resignFirstResponder
will be called just as the text field is losing focus.在 Swift 中,您需要将
UITextFieldDelegate
实现到您的ViewController
并从实现方法
:textFieldDidEndEditing
delegate然后您需要指定
textField
的delegate
:In
Swift
, you need to implement theUITextFieldDelegate
to yourViewController
and implement the methodtextFieldDidEndEditing
from thedelegate
:Then you need to asign the
delegate
of yourtextField
:我认为您已经实现了
UIKeyboardDidHideNotification
并且在这种情况下您使用类似
删除此代码的代码。
也在
textFieldShouldReturn
方法中写入相同的代码。 这也失去了焦点。i think you have implemented
UIKeyboardDidHideNotification
and in this event youuse code like
remove this code.
also same code write in
textFieldShouldReturn
method. this also lost focus.对于那些在 Swift 中遇到这个问题的人。 我们向 ViewController 的视图添加一个手势识别器,以便在点击视图时我们关闭文本字段。 重要的是不要取消对该视图的后续点击。
斯威夫特2.3
For those struggling with this in Swift. We add a gesture recognizer to the ViewController's view so that when the view is tapped we dismiss the textfield. It is important to not cancel the subsequent clicks on the view.
Swift 2.3