我试图检查当一个文本字段发生变化时,等效于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

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

and

@objc func textFieldDidChange(_ textField: UITextField) {

}

SWIFT 3和SWIFT 4.1

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

and

func textFieldDidChange(_ textField: UITextField) {

}

斯威夫特2.2

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

and

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

objective - c

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

和textFieldDidChange方法是

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

其他回答

到目前为止我处理它的方式:在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
}

如果你对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绑定:一个非常简单的技巧

你可以从UITextFieldDelegate中使用这个委托方法。它会随着角色的变化而爆发。

(Objective C) textField:shouldChangeCharactersInRange:replacementString:
(Swift) textField(_:shouldChangeCharactersInRange:replacementString:)

然而,这只会在更改之前触发(实际上,只有当你从这里返回true时,才会发生更改)。

这是如何使用Swift 3添加textField文本更改监听器:

将你的类声明为UITextFieldDelegate

override func viewDidLoad() {
    super.viewDidLoad()

    textField.delegate = self

    textField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:)), for: UIControlEvents.editingChanged)
}

然后只是传统地添加一个textFieldShouldEndEditing函数:

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { // do stuff
        return true 
}

现在在iOS13+上有一个UITextField委托方法可用

optional func textFieldDidChangeSelection(_ textField: UITextField)