0

I have the following code in a custom UIViewController:

let tapDismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismissTap)) func dismissTap() { print("Tap!") } override func viewDidLoad() { super.viewDidLoad() self.view.addGestureRecognizer(tapDismissTextField) self.friendsView.addGestureRecognizer(tapDismissTextField) } 

No "Tap!" lines are being printed, so the gesture recogniser isn't firing. What could be going wrong?

User Interaction Enabled is true accross all the views in this controller.

This view controller is embedded within a UINavigationController if that is of any help.

5
  • This is not the correct syntax, nor do I know how your code is not throwing an error, because there is no UITapGestureRecogniser, there's only UITapGestureRecognizer (with z, instead of s) Commented Feb 26, 2017 at 12:34
  • Apologies, those are all with a z Commented Feb 26, 2017 at 12:36
  • now, it's better. Spelling is really important. Commented Feb 26, 2017 at 12:37
  • Interesting, okay. I've left just self.view with the gesture recognizer, yet nothing is still printing. Could the problem lie outside this code? Commented Feb 26, 2017 at 12:44
  • posting an answer now Commented Feb 26, 2017 at 12:45

4 Answers 4

3

1. Move this line:

let tapDismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismissTap)) 

inside viewDidLoad(), because self outside the methods / function / closures returns an (NSObject) -> () -> YourClass() (an NSObject), not your class, which actually is the owner of view.

2. I recommend leaving only one view the GestureRecognizer

Sign up to request clarification or add additional context in comments.

Comments

1

You need to assign self only once your view is loaded.

var tapDismissTextField: UITapGestureRecognizer! 

and in your viewDidLoad method:

override func viewDidLoad() { super.viewDidLoad() tapDismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismissTap)) view.addGestureRecognizer(tapDismissTextField) } 

Comments

1

Add tapDismissTextField into viewdid load

override func viewDidLoad() { super.viewDidLoad() let tapDismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismissTap)) self.view.addGestureRecognizer(tapDismissTextField) self.friendsView.addGestureRecognizer(tapDismissTextField) } 

Comments

0

This is not the right way to get actions on the UITextField. I guess you did change the setUserInteraction to false to prevent UITextField behavior. right? Thats why your gesture recognizer doesn't work.

So, you need to give as you see we are in a dilemma now. we want the gesture to work and we dont want the UITextField to behave normaly. We need to know what you wonna achieve to help you better.

Info:

If you wonna show a custom view. for example UIPickerView set the inputView property for your UITextField and remove your gesture recognizer.

Work around:

You can use the tap gesture on UITextField superview. And, try to detect if the tap is inside the frame of the UITextField. example

Another work around:

Try to do your action on the textFieldDidBeginEditting delegate method. And do some tweaks to disable the keyboard. may be setting the inputView to nil.

So, we need to know exactly what do you want to achieve to give you the proper solution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.