iOS - Add Tap Gesture to UITextField

LakaLe_ picture LakaLe_ · Sep 5, 2014 · Viewed 10.1k times · Source

I have problem with add a Tap Gesture to my UITextField. Below code:

@IBAction func hanldeTap(recognizer: UITapGestureRecognizer) {
    println("works")
}

This action is associated with Tap Gesture Recognizer. In my TextField I have defined gestureRecognizer in OutletCollections. In my guess it should works. In described configuration gesture works e.x. for button or custom view.

Can you tell my what could go wrong and how can I fix this?

Answer

Allan  Macatingrao picture Allan Macatingrao · Sep 5, 2014

UITextField has delegate methods, you might want to consider implementing those. Or just add action event to your textfield.

For example in viewDidLoad

textField.addTarget(self, action:Selector("textDidBeginEditing"), forControlEvents: UIControlEvents.EditingDidBegin)

Then implement this:

func textDidBeginEditing(sender:UITextField) -> Void
{
   // handle begin editing event
}