1

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?

Hugo Tunius
  • 2,869
  • 24
  • 32
pmb
  • 2,327
  • 3
  • 30
  • 47
  • languageField.addTarget(self, action: #selctor(self.myTargetFunction(_:)), for: UIControlEvents.touchDown) – Sahil Jan 24 '17 at 12:07
  • Selector syntax is changed in Swift 3, Compare with this one http://stackoverflow.com/questions/38829151/swift3-ios-how-to-make-uitapgesturerecognizer-trigger-function/ This one is for GestureRecoginzer you need to change it with UItextField – Nirav D Jan 24 '17 at 12:08

2 Answers2

1

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")
}
Dhiraj Das
  • 174
  • 9
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

.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
    }
}
b.lyte
  • 6,518
  • 4
  • 40
  • 51