【问题标题】:How to check if UITextField text is empty when done editing完成编辑后如何检查 UITextField 文本是否为空
【发布时间】:2012-05-05 16:39:53
【问题描述】:

我目前有一个带有默认文本的 UITextField,并在开始编辑时设置为清除。我正在尝试这样做,以便在完成编辑时如果该字段为空或为零,则文本值将再次成为默认文本。我无法检测何时完成编辑或关闭键盘。谁能告诉我我想使用哪种检测方法以及如何实现它?非常感谢!


编辑


在我的情况下不能使用placeholder

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch uitextfield


    【解决方案1】:

    我认为您不需要自己处理“默认文本”。检查UITextField 类的placeholder 属性。

    更新

    所以placeholder 不适合您的情况。您是否尝试为您的UITextField 实现UITextFieldDelegate 方法并更改textViewDidEndEditing: 中的文本?

    为此,请在视图控制器中实现看起来对您的场景有用的UITextFieldDelegate 方法,并将UITextFielddelegate 设置为视图控制器(通过Interface Builder 或以编程方式)。在textViewDidEndEditing:方法中设置默认文本。

    您可以参考Text, Web, and Editing Programming Guide for iOS的“获取输入的文本并设置文本”部分。

    【讨论】:

    • 是的,我实际上需要在那里有默认文本,因为它的值在编辑后立即使用,我不能只使用占位符。我试过了,但我们的客户不希望这样:)
    • 添加了有关管理文本字段的官方文档的链接。
    【解决方案2】:

    当您收到文本字段返回时的委托调用时,使用sender 参数检查文本(sender.text),如果等于@"",则设置您的默认文本。

    【讨论】:

      【解决方案3】:

      您必须使用文本字段委托

      -(void)textFieldDidEndEditing:(UITextField *)textField;
      

      在这个委托中进行这样的检查

      if ( [textField.text length] == 0 )
      {
          // the text field is empty do your work
      } else {
          ///  the text field is not empty 
      }
      

      【讨论】:

        【解决方案4】:
        - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
        {
            if (range.location == 0 && string.length == 0)
            {
                //textField is empty
            }
            return YES;
        }
        

        【讨论】:

        • 如果用户突出显示从 0 索引开始的文本子集然后删除,这将不起作用。
        • @AlexisCandelaria,我又发布了一个答案
        【解决方案5】:

        您还需要检查删除的范围是否是文本的整个长度。

        func textField(_ textField: UITextField, shouldChangeCharactersIn range:  NSRange, replacementString string: String) -> Bool {
            if !(range.location == 0 && string.count == 0 && range.length == textField.text?.count) {
              // Text field is empty
            }
            return true
        }
        

        【讨论】:

          【解决方案6】:

          我使用两种方法。我的选择取决于应用程序的业务逻辑。

          1) 我在 shouldChangeCharactersIn 中应用更改后检查结果文本:

          func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
                  var text = textField.text ?? ""
          
                  let startIndex = text.index(text.startIndex, offsetBy: range.location)
                  let endIndex = text.index(text.startIndex, offsetBy: range.location + range.length)
                  text = text.replacingCharacters(in: startIndex..<endIndex, with: string)
                  if text.count == 0 {
                      // textField is empty
                  } else {
                      // textField is not empty
                  }
                  return true
              }
          

          2) 我使用editingChanged 操作:

          @IBAction private func textFieldDidChange(_ sender: UITextField) {
                  if (sender.text?.count ?? 0) == 0 {
                      // textField is empty
                  } else {
                      // textField is not empty
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-10-29
            • 2017-05-25
            • 1970-01-01
            • 2015-07-13
            • 1970-01-01
            • 2015-09-03
            • 1970-01-01
            相关资源
            最近更新 更多