No matter what I do, I always get this error.
This is inside my cellForItemAtIndexPath
function:
cell.postCell.profileImage.userView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "pressedUserImage:"))
and the action method:
func pressedUserImage(userView: UIImageView) {
print(userView.superview) // error happens here.
Now of course, my userView is within a deep view hierarchy but it still has to work. For some reason it doesn't. Even if I do userImage.superview?.superview
and so forth, I still get the error.
I tried using #selector(pressedUserImage(_:)
instead, but no luck. Tried Selector("pressedUserImage:")
. Nothing.
Where am I going wrong here?
Update, here's the cellForItemAtIndexPath
function:
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
is basically a UIView
that contains the image, name, text.
Here's the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[project.Posts pressedUserImage]: unrecognized selector sent to instance 0x7a9a65e0'
It looks like the problem is with your function signature. It needs to accept the gesture recognizer as a parameter, not the containing view.
Like:
func pressedUserImage(_ sender: UITapGestureRecognizer) { ... }