Swift 3:发送到手势识别器实例的无法识别的选择器
Swift 3: unrecognized selector sent to instance for gesture recognizer
无论我做什么,我总是会遇到这个错误。
这是我的 cellForItemAtIndexPath
函数:
cell.postCell.profileImage.userView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "pressedUserImage:"))
及动作方法:
func pressedUserImage(userView: UIImageView) {
print(userView.superview) // error happens here.
现在当然,我的 userView 位于深层视图层次结构中,但它仍然必须工作。出于某种原因,它没有。即使我执行 userImage.superview?.superview
等等,我仍然会收到错误消息。
我尝试使用 #selector(pressedUserImage(_:)
代替,但没有运气。尝试过 Selector("pressedUserImage:")
。没有什么。
我哪里错了?
更新,这里是cellForItemAtIndexPath
函数:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! Posts
cell.postCell.profileImage.userView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "pressedUserImage:"))
cell.postCell.buttonView.sharePost.addTarget(self, action: #selector(pressedShareButton), for: .touchUpInside)
return cell
}
postCell
基本上是一个包含图像、名称、文本的 UIView
。
这是错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[project.Posts pressedUserImage]: unrecognized selector sent to instance 0x7a9a65e0'
看起来问题出在您的函数签名上。它需要接受手势识别器作为参数,而不是包含视图。
喜欢:
func pressedUserImage(_ sender: UITapGestureRecognizer) { ... }
正如 Joshua 所指出的,您的函数签名有误。您需要正确设置参数的类型,并添加下划线使参数未命名。
除了他的更改之外,如果您的 class 不是 Objective-C,您还应该将函数标记为 Objective-C class:
@objc func pressedUserImage(_ sender: UITapGestureRecognizer) { ... }
无论我做什么,我总是会遇到这个错误。
这是我的 cellForItemAtIndexPath
函数:
cell.postCell.profileImage.userView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "pressedUserImage:"))
及动作方法:
func pressedUserImage(userView: UIImageView) {
print(userView.superview) // error happens here.
现在当然,我的 userView 位于深层视图层次结构中,但它仍然必须工作。出于某种原因,它没有。即使我执行 userImage.superview?.superview
等等,我仍然会收到错误消息。
我尝试使用 #selector(pressedUserImage(_:)
代替,但没有运气。尝试过 Selector("pressedUserImage:")
。没有什么。
我哪里错了?
更新,这里是cellForItemAtIndexPath
函数:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! Posts
cell.postCell.profileImage.userView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "pressedUserImage:"))
cell.postCell.buttonView.sharePost.addTarget(self, action: #selector(pressedShareButton), for: .touchUpInside)
return cell
}
postCell
基本上是一个包含图像、名称、文本的 UIView
。
这是错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[project.Posts pressedUserImage]: unrecognized selector sent to instance 0x7a9a65e0'
看起来问题出在您的函数签名上。它需要接受手势识别器作为参数,而不是包含视图。
喜欢:
func pressedUserImage(_ sender: UITapGestureRecognizer) { ... }
正如 Joshua 所指出的,您的函数签名有误。您需要正确设置参数的类型,并添加下划线使参数未命名。
除了他的更改之外,如果您的 class 不是 Objective-C,您还应该将函数标记为 Objective-C class:
@objc func pressedUserImage(_ sender: UITapGestureRecognizer) { ... }