Search code examples
iosswiftswift3uitextfieldskyfloatinglabeltextfield

textfield on touch not working


I am trying to set ontouch listener on UITextField but it not working at all here is my code:

class SettingsVC: BaseVC {


    @IBOutlet weak var languageField: SkyFloatingLabelTextField!
    @IBOutlet weak var btnburgerMenu: UIButton!
    @IBOutlet weak var headerLbl: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        languageField.addTarget(self, action: "myTargetFunction:", for: UIControlEvents.touchDown)
    }

    func myTargetFunction(textField: SkyFloatingLabelTextField) {
        print("test")
    }

}

Here is the error I have

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IFO.SettingsVC myTargetFunction:]: unrecognized selector sent to instance 0x7fac8b61ff30'

What's the reason of the exception?


Solution

  • It should be like so -

    languageField.addTarget(self, action: #selector(YourViewController.myTargetFunction(sender:)), for: UIControlEvents.touchDown)
    

    or use like

    languageField.addTarget(self, action: #selector(self.myTargetFunction(_:)), forControlEvents: .touchDown)
    

    and call the function like

    func myTargetFunction(_ textField: SkyFloatingLabelTextField) {
        print("test")
    }