0

After viewing different options on the Internet, I never found a working implementation option how to do it. I just want to add listener to UITextField. Have some code

class RegisterViewController: UIViewController, UITextFieldDelegate

  @IBOutlet weak var textRegion: UITextField!

textRegion.addTarget(self, action: Selector(("myTargetFunction:")), for: UIControlEvents.touchDown)
    func myTargetFunction() {
        print("It works!")
    }

but it does not work, have THREAD 1 : SIGNAL SIGABRT

Rashwan L
  • 38,237
  • 7
  • 103
  • 107

1 Answers1

1

You have the wrong syntax for your selector. Use this one instead #selector(myTargetFunction).

The entire code:

In your viewDidLoad:

textRegion.addTarget(self, action: #selector(myTargetFunction), for: UIControlEvents.touchDown)

And then declare the function:

func myTargetFunction(textField: UITextField) {
    print("touchDown for \(textField.tag)")
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107