0

My code:

override func viewDidLoad() {
    super.viewDidLoad()

    //Adding the long gesture
    let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))

    //Adding the gesture to the button
    hydrogenBoxButton.addGestureRecognizer(longGesture)

    //Adding a tag for the button
    hydrogenBoxButton.tag = 1
}

And then my function being called in the selector:

 @objc func longTap(sender: UIButton) {

        if sender.tag == 1 {
            print("Hydrogen long tap worked!")
        } else if sender.tag == 2 {
            print("Helium long tap worked!")
        }
    }

However, when I run this and try long pressing on the button I get this error:

-[UILongPressGestureRecognizer tag]: unrecognized selector sent to instance 0x7fbc5ce00680

I don't know if it's because I have named the tags wrong or something with the way I created the LongPressGestureRecognizer.

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Leo Marcinkus
  • 78
  • 1
  • 7

1 Answers1

0

Parameter is of type UILongPressGestureRecognizer

@objc func longTap(sender: UILongPressGestureRecognizer ) {
        let tag = sender.view!.tag
        if tag == 1 {
            print("Hydrogen long tap worked!")
        } else if tag == 2 {
            print("Helium long tap worked!")
        }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87