gpt4 book ai didi

ios - 如何检查 UITextField 何时发生变化?

转载 作者:行者123 更新时间:2023-11-30 11:47:37 24 4
gpt4 key购买 nike

我正在尝试检查文本字段何时发生更改,也相当于用于 textView 的函数 - textViewDidChange 到目前为止我已经这样做了:

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

哪一种有效,但是一旦按下文本字段,topRightButton就会启用,我希望它仅在实际输入文本时才启用?

最佳答案

SWIFT

swift 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
}

objective-C

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

而textFieldDidChange方法是

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

关于ios - 如何检查 UITextField 何时发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48644445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com