Search code examples
iosswiftuitapgesturerecognizer

addTarget function not firing after addGestureRecognizer function fires


\\Action 1
textfield.addTarget(self, action: #selector(self.didChangeText(textField:)), for: .editingChanged)
textfield.tag = self.numarr

\\Action 2
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
textfield.addGestureRecognizer(tap)
textfield.isUserInteractionEnabled = true

Both functions fire but individually. When together the gesture function fires but the 2nd (addTarget) doesn't fire. Any fixes?


Solution

  • Tap gesture is not suitable for UITextField you may use textDidBeginEditing. Still if you want a tap effect you may need to use some transparent overlay using UIImageView or UIButton. UITextField has delegate methods, you might want to consider implementing those. Or just add action event to your textfield.

    textField.addTarget(self, action: #selector(self.textDidBeginEditing(sender:)), for: UIControlEvents.editingDidBegin)
    

    Then implement this:

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

    Similarly you can place a transparent button and inside the action of that button you can place your desired code and also hide the button from the view till the control change from UITextField.