Search code examples
swiftxcodeuibuttontagsuilongpressgesturerecogni

Trying to call a long tap gesture and run a command but running into a SIGABRT error with the selector in the function. What isn't connected?


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.


Solution

  • 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!")
            }
    }