Reputation: 363
An image sends an action to an @objc C function when pressed:
let imageView = UIImageView()
imageView.tag = 3
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ItemAction)))
imageView.image = UIImage(named: (itemDictionary[character.Equipment[imageView.tag]]!.image))
overlayView.addSubview(imageView)
this is the function that should be called:
@objc func ItemAction(sender: UIImageView!) {
print(sender.tag)
print("Item pressed from sender ")
}
the function runs, but when it comes to printing the sender tag I get an error message and the program quits:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x600002b42a00'
The whole setup with a UIButton works. How can I read the tag of an UIImageView in an external function?
Upvotes: 0
Views: 133
Reputation: 10012
here you go
@objc func itemAction(_ sender: UITapGestureRecognizer){
if let tag = sender.view?.tag{
print("ImageView tag \(tag)")
}
}
in your function change your method signature to
@objc func ItemAction(_ sender: UITapGestureRecognizer)
Upvotes: 2