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

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

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


当前回答

斯威夫特3

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

其他回答

也许使用RxSwift ?

need

pod 'RxSwift',    '~> 3.0'
pod 'RxCocoa',    '~> 3.0'

明显地添加导入

import RxSwift
import RxCocoa

你有一个textfield: UITextField

let observable: Observable<String?> = textField.rx.text.asObservable()
observable.subscribe(
            onNext: {(string: String?) in
                print(string!)
        })

你有其他3种方法。

onError oncomplete onDisposed onNext

txf_Subject.addTarget(self, action:#selector(didChangeFirstText), for: .editingChanged)

@objc func didChangeText(textField:UITextField) {
    let str = textField.text
    if(str?.contains(" "))!{
        let newstr = str?.replacingOccurrences(of: " ", with: "")
        textField.text = newstr
    }
}

@objc func didChangeFirstText(textField:UITextField) {
    if(textField.text == " "){
        textField.text = ""
    }
}

到目前为止我处理它的方式:在UITextFieldDelegate中

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    // text hasn't changed yet, you have to compute the text AFTER the edit yourself
    let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)

    // do whatever you need with this updated string (your code)


    // always return true so that changes propagate
    return true
}

Swift4版本

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
    return true
}

你可以通过“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
        }
    }
}

斯威夫特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) { 

}