一尘不染

如何检查UITextField何时更改?

swift

我正在尝试检查文本字段何时更改,也等同于用于textView的功能- textViewDidChange到目前为止,我已经做到了:

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

哪种方法有效,但是topRightButton只要在按下文本字段时就启用,我希望仅在实际键入文本时才启用它?


阅读 281

收藏
2020-07-07

共1个答案

一尘不染

迅速

斯威夫特4.2

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

@objc func textFieldDidChange(_ textField: UITextField) {

}

SWIFT 3和Swift 4.1

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

func textFieldDidChange(_ textField: UITextField) {

}

SWIFT 2.2

textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)

func textFieldDidChange(textField: UITextField) {
    //your code
}

目标C

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

和textFieldDidChange方法是

-(void)textFieldDidChange :(UITextField *) textField{
    //your code
}
2020-07-07