检测UITextField失去焦点 [英] Detect UITextField Lost Focus

查看:103
本文介绍了检测UITextField失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这里 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!为什么?!?

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)!

谢谢。

推荐答案

要处理外部文本字段,您可以在视图控制器中覆盖 touchesBegan

To handle tapping outside text fields you can override touchesBegan in your view controller:

- (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
    }

    ...
}

这篇关于检测UITextField失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆