3

I read a lot try to find a way to handle uitextfield click but nothing working for me

What I want:

I want add tap on UITextField and open dialog

What I tried:

class CreateMessagesViewController: UIViewController {

    @IBOutlet weak var testField: UITextField!
    @IBOutlet weak var testView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
//        testField.addGestureRecognizer(tap)
        testView.addGestureRecognizer(tap)

        testField.addTarget(self, action: #selector(onTapped(_:)), for: .touchDown)
    }

    func onTapped(_ sender : UITextField){

        print("Hello World")
    }

    func handleTap(_ sender: UITapGestureRecognizer) {
        print("Hello World")
    }
}

As you can see I tried 2 ways:

  1. UITapGestureRecognizer
  2. selector

Just for the test I add simple UIView to make sure tap is working and it worked.

Why it doesn't work on uiTextField? Is there anything I have missed?

fish40
  • 5,738
  • 17
  • 50
  • 69

7 Answers7

4

Instead of adding gesture recognizer you can write the code for opening dialog in

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    openDialog()//Function which will open the dialog
    return false
}

I think it should have the same effect as you want.

Pranav Pravakar
  • 205
  • 3
  • 7
1

1)add gesture recognizer:

let tapGR = UITapGestureRecognizer(target: self, action: #selector(someFunc))
tapGR.delegate = self
textField.addGestureRecognizer(tapGR)

2)make sure that you allow your tap handler when it is necessary:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true //or add your custom check here if you need
}

Without of the second part you get a buggy textField where sometimes tap gesture doesn't work, sometimes default gestures not handled!

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49
1

There is a simpler way to achieve this. You should conform your view controller class to UITextFieldDelegate and implement textFieldDidBeginEditingin your class.

func textFieldDidBeginEditing(_ textField: UITextField){
       //show popup here
}

If you are having multiple text fields, assign a tag to your text field in the storyboard(or in the code) to recognize the text field.

func textFieldDidBeginEditing(_ textField: UITextField){
// check your tag here
   if textField.tag == 0 {
            //show your popup here
   }
}
frlzjosh
  • 410
  • 5
  • 17
Vkharb
  • 856
  • 11
  • 19
0

Add GestureRecognizer to UITextField superview and check if help:

testField.superview.addGestureRecognizer(tap)
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0

I created sample one for your question both Swift and Objective C but it not working.I used the Tap Gesture recognizer code for textField but it not even called.

Not Worked below Code in Swift and Objective C

Swift

let tap = UITapGestureRecognizer(target: self,action: #selector(handleTaponTextField(_:)))
tap.numberOfTapsRequired = 1
tap.delegate = self
txtFldTapMe.isUserInteractionEnabled = true
txtFldTapMe.addGestureRecognizer(tap)

func handleTaponTextField(_ sender: UITapGestureRecognizer)
{
    print("Tap is handled here!")
}

Objective C

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.delegate = self;
txtfldTapME.userInteractionEnabled = YES;
[txtfldTapME addGestureRecognizer:tapGesture];

func handleTaponTextField(_ sender: UITapGestureRecognizer)
{
    print("Tap is handled here!")
}

Then I tried with below addTarget action method for textField and it works fine.

Worked below Code in Swift and Objective C

Swift

txtFldTapMe.addTarget(self, action: #selector(textFieldEditDidBegin(_:)), for: .editingDidBegin)

func textFieldEditDidBegin(_ textField: UITextField) {
    print("Text editing begin here!")
}

See the methods

enter image description here

You should use above methods of textField

Objective C

[txtfldTapME addTarget:self action:@selector(editingDidBeginStarted:) forControlEvents: UIControlEventEditingDidBegin];   

-(void)editingDidBeginStarted:(UITapGestureRecognizer *)sender{
  NSLog(@"Editing started here");
}

See the below textField methods

enter image description here

user3182143
  • 9,459
  • 3
  • 32
  • 39
0

Try below code

yourTextField.addTarget(self, action: #selector(didChangeText), for: .editingChanged)
    addSubview(view)

@objc func didChangeText(textField:UITextField) {
    let str = textField.text
        //your functionality here
}
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25
-1

It's not possible. Because Textfield delegate method call.

Nidhi Patel
  • 1,222
  • 11
  • 17