iphone - 检测 UITextField 失去焦点

标签 iphone objective-c ipad uitextfield protocols

我用谷歌和这里搜索了很多,但没有任何用处。

我有两个文本框,我无法识别哪个失去了焦点。
我尝试了所有选项,但一无所获。

这里是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/

相关文章:

iphone - block 键盘输入

iphone - 如何将图标添加到 iPhone 应用程序?

iphone - 在 iOS 应用程序中使用 SSL 进行登录

iphone - 通过 (id)sender 访问 UIButton

ios - 在 swift 3 中显示来自自定义单元格的警报

iphone - 被触摸时更新 View

iphone - 核心数据获取不一致

ios - 使用 uiTextfield 的许多 subview 键盘上下移动

ios - 由于未捕获的异常 '_HKObjectValidationFailureException' 而终止应用程序

iphone - 如何从另一个应用程序打开我的 ios 应用程序中的图像?