Reputation: 2708
I am trying to attach a gesture recognizer on a button and on an image view in a dynamic tableView cell, but I get error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton nameOfuserTappedWithGestureRecgonizer:]: unrecognized selector sent to instance 0x7ff34253f5a0'
protocol MediaTableViewCellDelegate: class {
func didClickProfileImageOf(cell: MediaTableViewCell)
func didClickProfileNameOf(cell: MediaTableViewCell)
}
class MediaTableViewCell: UITableViewCell {
weak var delegate: MediaTableViewCellDelegate?
@IBOutlet weak var mediaImageView: UIImageView! //the large image
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var fullNameButton: UIButton!
var tapGestureRecognizerProfileImage = UITapGestureRecognizer()
var tapGestureRecognizerProfileName = UITapGestureRecognizer()
override func awakeFromNib() {
super.awakeFromNib()
initialize()
}
private func initialize() {
tapGestureRecognizerProfileImage.addTarget(self.profileImageView, action: #selector(MediaTableViewCell.imageTapped(gestureRecgonizer:)))
self.addGestureRecognizer(tapGestureRecognizerProfileImage)
tapGestureRecognizerProfileName.addTarget(self.shareButton, action: #selector(MediaTableViewCell.nameOfuserTapped(gestureRecgonizer:)))
self.addGestureRecognizer(tapGestureRecognizerProfileName)
}
func imageTapped(gestureRecgonizer: UITapGestureRecognizer) {
delegate?.didClickProfileImageOf(cell: self)
}
func nameOfuserTapped(gestureRecgonizer: UITapGestureRecognizer) {
delegate?.didClickProfileNameOf(cell: self)
}
}//end class
Upvotes: 0
Views: 1856
Reputation: 2253
Your question title is misleading -- You are adding gesture recognizer to the UIElements, not the whole cell.
Buttons don't need a gesture recognizer since they subclass from UIControl.
private func initialize() {
let imageTapGesture = UITapGestureRecognizer(target: self, action: #selector(profileTapped))
profileImageView.addGestureRecognizer(imageTapGesture)
// imageviews by default aren't interactable
profileImageView.isUserInteractionEnabled = true
fullNameButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}
@objc private func profileTapped() {
delegate?.didClickProfileImageOf(cell: self)
}
@objc private func buttonTapped() {
delegate?.didClickProfileNameOf(cell: self)
}
-- Updated per comment --
protocol MediaTableViewCellDelegate: class {
func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileImage: Bool)
func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileName: Bool)
}
Upvotes: 2
Reputation: 236
I think that you shouldn't add UIGestureRecognizer for a button, cuz it has already touch event.
Just add this code for button:
fullNameButton.addTarget(self, action: #selector(some selector), for: .touchUpInside)
As for profileImageView add this lines of code:
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(some selector))
profileImageView.isUserInteractionUnabled = true
profileImageView.addGestureRecognizer(tapRecognizer)
Upvotes: 1
Reputation: 864
You are mixing up the argument of addTarget
with the caller of addGestureRecognizer
.
The target of a gesture recognizer's addTarget
is the class that implements the selector (the closure passed as action
). Which in this case is self
.
addGestureRecognizer
adds the recognizer to the view itself.
So you want:
tapGestureRecognizerProfileImage.addTarget(self, action: #selector(MediaTableViewCell.imageTapped(gestureRecgonizer:)))
self.profileImageView.addGestureRecognizer(tapGestureRecognizerProfileImage)
Upvotes: 0