我试图检查当一个文本字段发生变化时,等效于textView - textViewDidChange的函数,到目前为止我已经做到了这一点:

  func textFieldDidBeginEditing(textField: UITextField) {
        if self.status.text == "" && self.username.text == "" {
            self.topRightButton.enabled = false
        } else {   
            self.topRightButton.enabled = true
        }
    }

哪种类型的工作,但topRightButton一经文本字段被按下就被启用,我想它只在文本实际输入时才被启用?


当前回答

斯威夫特4.2

把这个写在viewDidLoad中

// to detect if TextField changed
TextField.addTarget(self, action: #selector(textFieldDidChange(_:)),
                                   for: UIControl.Event.editingChanged)

把这个写在viewDidLoad外面

@objc func textFieldDidChange(_ textField: UITextField) {
    // do something
}

你可以通过uicontrol。event。editingdidbegin改变事件或者任何你想检测的东西。

其他回答

斯威夫特4

在viewDidLoad ():

    //ADD BUTTON TO DISMISS KEYBOARD

    // Init a keyboard toolbar 
    let toolbar = UIView(frame: CGRect(x: 0, y: view.frame.size.height+44, width: view.frame.size.width, height: 44))
    toolbar.backgroundColor = UIColor.clear

    // Add done button
    let doneButt = UIButton(frame: CGRect(x: toolbar.frame.size.width - 60, y: 0, width: 44, height: 44))
    doneButt.setTitle("Done", for: .normal)
    doneButt.setTitleColor(MAIN_COLOR, for: .normal)
    doneButt.titleLabel?.font = UIFont(name: "Titillium-Semibold", size: 13)
    doneButt.addTarget(self, action: #selector(dismissKeyboard), for: .touchUpInside)
    toolbar.addSubview(doneButt)

    USDTextField.inputAccessoryView = toolbar

增加这个函数:

    @objc func dismissKeyboard() {
      //Causes the view (or one of its embedded text fields) to resign the first responder status.
      view.endEditing(true)
    }

斯威夫特5.0

textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                          for: .editingChanged)

及处理方法:

@objc func textFieldDidChange(_ textField: UITextField) {

}

斯威夫特4.0

textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                          for: UIControlEvents.editingChanged)

及处理方法:

@objc func textFieldDidChange(_ textField: UITextField) {

}

斯威夫特3.0

textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)

及处理方法:

func textFieldDidChange(textField: UITextField) { 

}

你可以通过“shouldchangecharter”委托或者“begin and end”文本字段委托来管理编辑。别忘了设置委托

我设法做到这一点使用“did开始和结束编辑”下面。

//MARK:- TextViewDelegate
extension ViewController: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        let count = self.tfEmail.text?.count ?? 0
        if textField == self.tfEmail {
            if count == 0{
              //Empty textfield
            }else{
              //Non-Empty textfield
            }
        }
    }
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == self.tfEmail{
            //when user taps on textfield
        }
    }
}

如果你对SwiftUI解决方案感兴趣,这对我来说是有效的:

 TextField("write your answer here...",
            text: Binding(
                     get: {
                        return self.query
                       },
                     set: { (newValue) in
                        self.fetch(query: newValue) // any action you need
                                return self.query = newValue
                      }
            )
  )

我不得不说这不是我的主意,我在这个博客上看到的:SwiftUI绑定:一个非常简单的技巧

斯威夫特4

符合 UITextFieldDelegate。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    // figure out what the new string will be after the pending edit
    let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)

    // Do whatever you want here


    // Return true so that the change happens
    return true
}