code-8
code-8

Reputation: 58770

How can I detect when UITextField changed and trigger an alert?

I'm trying to detect when the user change the input field.

I created this function

@objc func textFieldDidChange(_ textField: UITextField) {

    let alert = UIAlertController(title: "Did you bring your towel?", message: "It's recommended you bring your towel before continuing.", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

    self.present(alert, animated: true)

}

I added my Target like this

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

I kept getting this error

Use of unresolved identifier 'textFieldDidChange(passWord:)'

enter image description here

How do I prevent this error to happen ?

Upvotes: 0

Views: 214

Answers (2)

Isha Patel
Isha Patel

Reputation: 29

easiest way for this, simply create password textfield event.

Upvotes: 0

Ashley Mills
Ashley Mills

Reputation: 53181

Your selector is wrong…

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

See https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

Upvotes: 1

Related Questions