我用谷歌和这里搜索了很多,但没有任何用处。
我有两个文本框,我无法识别哪个失去了焦点。
我尝试了所有选项,但一无所获。
这里是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!为什么?!?
如何检测失去焦点?每次我按下键盘按钮时都会调用此方法,不仅在最后!
有没有其他选择?
编辑:
我不想从文本切换,但我想在我点击屏幕时检测到失去焦点。 (键盘将关闭或不关闭,并且文本字段中不存在插入符号)!
谢谢。
最佳答案
要处理文本字段外的点击,您可以覆盖 View Controller 中的 touchesBegan
:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [[event allTouches] anyObject];
if ([textField1 isFirstResponder] && (textField1 != touch.view))
{
// textField1 lost focus
}
if ([textField2 isFirstResponder] && (textField2 != touch.view))
{
// textField2 lost focus
}
...
}
关于iphone - 检测 UITextField 失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6369758/