【问题标题】:UITextField calls textFieldDidEndEditing when cleared but `text` property has dataUITextField 在清除时调用 textFieldDidEndEditing 但 `text` 属性有数据
【发布时间】:2013-09-10 04:48:16
【问题描述】:

有没有人注意到UITextField 在按下清除按钮后调用textFieldDidEndEditingtext 属性仍然有旧数据?

我不确定我可以在这里提供什么代码示例。如果这很重要,我正在使用情节提要。

现在我必须依靠从主窗体的“提交”按钮上的所有编辑控件获取数据。但理想情况下,我更愿意在 textFieldDidEndEditing 处理程序中收集数据。

有没有更好的解决方法?

我使用的是 iOS 6。

更新:基本上这是我在表格上的内容

  • UITextFieldUiButton 在表单上。
  • 通过在UITapGestureRecognizer 的处理程序中调用resignFirstResponder 关闭键盘

重现问题的步骤:

  • 单击编辑控件。输入一些文字。
  • 在文本控件之外点按。
  • textFieldDidEndEditing 被调用。属性 .text 具有我输入的值。一切顺利。
  • 再次点击编辑控件。
  • 点击清除按钮。
  • textFieldDidEndEditing 再次被调用。但是.text 的属性仍然有我刚刚删除的值!
  • 现在您可以看到光标在 UITextField 内闪烁,点击表单上的按钮。
  • 键盘被 textFieldDidEndEditing 关闭,从未调用过。

我明天会在 GitHub 上上传示例项目。

【问题讨论】:

    标签: ios ios6 uitextfield uitextfielddelegate


    【解决方案1】:

    我遇到了完全相同的问题。至少在我的情况下,这是由于在self.view 中添加了UITapGestureRecognizer(以允许在UITextField 之外点击时关闭键盘)并在手势识别器上设置cancelsTouchesInView=NO。我已经设置了该属性,以便在视图中其他地方的TTTAttributesLabel 上进行超链接。

    我的解决方法是观察键盘显示和隐藏通知,并相应地切换该属性:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowNotification:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideNotification:) name:UIKeyboardDidHideNotification object:nil];
    

    (注册通知)

    - (void)keyboardDidShowNotification:(NSNotification*)notification
    {
        tapGestureRecognizer.cancelsTouchesInView = YES;
    }
    
    - (void)keyboardDidHideNotification:(NSNotification *)notification
    {
        tapGestureRecognizer.cancelsTouchesInView = NO;
    }
    

    (处理通知)

    就行为而言,唯一的问题是超链接在显示键盘时仍然不起作用:触摸它只会关闭键盘,而不是将触摸转发给链接处理程序。但我可以忍受。关闭键盘后,链接工作正常。

    【讨论】:

      【解决方案2】:

      先检查UITextFieldDelegate是否被赋值,然后

      实现textFieldShouldClear 委托并在此处编写代码清除您的文本字段

      为此,您必须设置clearButtonMode 属性,

      yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
      yourTextField.delegate = self;
      

      然后实现textFieldShouldClear委托

      .h 文件

      @interface myViewController: UIViewController <UITextFieldDelegate>{
      }
      

      .m 文件

      -(BOOL)textFieldShouldClear:(UITextField *)textField
      
          yourTextFeild.text = @"";
          return YES;
      }
      

      【讨论】:

      • 谢谢!我已经完成了这一切。不幸的是,这并没有什么不同。另外我应该提到textFieldDidEndEditing被称为之前 textFieldShouldClear
      【解决方案3】:

      在这里试试:

      -(BOOL) textFieldShouldReturn:(UITextField*) textField
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多