简体   繁体   中英

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?

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")
}

.touchDown is actually quite useless as it doesn't always fire. I needed a way for dialog to appear every time UITextField was touched. I used this instead:

class DateTextField: DefaultTextField, UITextFieldDelegate {
    ...
    override init() {
        super.init()
        self.delegate = self // delegate to handle textFieldShouldBeginEditing below
        inputView = UIView() // disable the input view
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        // Code to display my date picker dialog
        return false // return false to disable textfield editing
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM