apscience
apscience

Reputation: 7253

Swift: Accessing outlet in action handler gives "unrecognized selector sent to instance"

Inside my ViewController, I have two UITextFields, hooked up as outlets via the storyboard / IB:

@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!

I have hooked up an action for "Editing Changed" for one UITextField:

@IBAction func textField1Change(_ sender: UITextField) {
    textField2.text = "Changed"
}

However, I get a unrecognized selector sent to instance error when I type something into textField1

I can't even do print(textField), that gives me the error too. What am I doing wrong?

Upvotes: 0

Views: 219

Answers (1)

Nazmul Hasan
Nazmul Hasan

Reputation: 10590

The textField1 is probably connected to some other action? Check the outlets of the textField1 to make sure.

Or try this way :

 func viewDidLoad() {
      super.viewDidLoad()
      textField1.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
    }

 func textFieldDidChange(_ textfield: UITextField) {
      textField2.text = "Changed"
    }

Upvotes: 2

Related Questions