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:
- UITapGestureRecognizer
- 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?