UITapGestureRecognizer没有在Swift 3中触发
问题描述:
我在自定义 UIViewController
中有以下代码:
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?
在此控制器的所有视图中启用的用户交互 true 。
User Interaction Enabled is true accross all the views in this controller.
如果有任何帮助,这个视图控制器嵌入在 UINavigationController
中。
This view controller is embedded within a UINavigationController
if that is of any help.
答
您需要分配一次你的看法被载入自
You need to assign self only once your view is loaded.
var tapDismissTextField: UITapGestureRecognizer!
并在你的viewDidLoad方法中:
and in your viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
tapDismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismissTap))
view.addGestureRecognizer(tapDismissTextField)
}