检测 UITextField 失去焦点
我在谷歌上搜索了很多,但没有什么用处。
我有两个文本字段,但我无法识别哪个文本字段失去了焦点。
我尝试了所有选项,但没有任何结果。
这里的 textFieldDidEndEditing
:
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"%@", [textField state]);
NSLog(@"%d", [textField isSelected]);
NSLog(@"%d", [textField isFirstResponder]);
NSLog(@"%d", [textField isHighlighted]);
NSLog(@"%d", [textField isTouchInside]);
if ( ![textField isFirstResponder] || ![textField isSelected] ) {
//if ( [textField state] != UIControlStateSelected) {
NSLog(@"not selected!");
[...]
// remove view / etc...
}
}
所有 NSLog 返回 0!为什么?!?
如何检测失去焦点?每次我按下键盘按钮时都会调用此方法,不仅仅是在最后!
还有其他选择吗?
编辑:
我不想从文本切换,但我想在我单击屏幕上的任何内容时检测到失去焦点。 (键盘是否会关闭,并且文本字段中不存在插入符号)!
谢谢。
i searched a lot googling and heree, but nothing useful.
I have two textfields and i don't able to recognize which one lost the focus.
I tried all options, but nothing.
Here the textFieldDidEndEditing
:
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"%@", [textField state]);
NSLog(@"%d", [textField isSelected]);
NSLog(@"%d", [textField isFirstResponder]);
NSLog(@"%d", [textField isHighlighted]);
NSLog(@"%d", [textField isTouchInside]);
if ( ![textField isFirstResponder] || ![textField isSelected] ) {
//if ( [textField state] != UIControlStateSelected) {
NSLog(@"not selected!");
[...]
// remove view / etc...
}
}
All NSLog returns 0! Why?!?
How can i detect lost focus? This method has called every time that i press a keyboard button, non only at the end!
Is there any alternatives?
EDIT:
I don't want to switch from texts but i want to detect lost focus when i click anyways on screen. (the keyboard will dismiss or not and the caret is not present on textfield)!
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要处理点击外部文本字段,您可以在视图控制器中覆盖
touchesBegan
:To handle tapping outside text fields you can override
touchesBegan
in your view controller:带有标签的 UITextField 在 textFieldShouldReturn 方法中失去了焦点
这将帮助您从一个 TextField 转到另一个 TextField...只需在所有 TextField 中递增设置标签,例如:0,1,2,3...等
The UITextField with tag had lost focus in textFieldShouldReturn method
This will help you to go from one TextField to another....just set tag incremently in all TextFields ex : 0,1,2,3....etc
这不是直接答案,因为您询问了失去焦点时如何处理。我认为有时候有明确的保存和取消按钮来关闭是很好的。特别是在文本视图中,您希望保留返回键以供其预期用途。
该类向键盘添加了一个带有“完成”和“取消”按钮的工具栏。我现在可以在 iOS 8 中使用它。我对 iOS 还很陌生,所以可能有更好的方法来做到这一点。始终乐于接受有关如何改进的建议。
DismissableTextView.h...
DismissableTextView.m...
This isn't a direct answer since you asked about how to handle when losing focus. I think there are times when it's nice to have explicit save and cancel buttons to dismiss. Especially on a text view where you want to preserve the return key for it's intended use.
This is a class which adds a toolbar to the keyboard with "Done" and "Cancel" buttons. I have this working in iOS 8 right now. I am pretty new to iOS so there might be better ways to do this. Always open to suggestions on how to improve.
DismissableTextView.h...
DismissableTextView.m...
创建文本字段时,为它们分配不同的标签:
在 - (void)textFieldDidEndEditing:(UITextField *)textField 方法中,您可以通过查询其标签来判断哪个文本字段结束了编辑:
When you are creating the text fields, assign different tags to them:
In your - (void)textFieldDidEndEditing:(UITextField *)textField method, you can tell which textfield ended editing by querying its tag: