0

I am trying to use the long press button function but am not sure how to pass the tag of the button to the function. I have an array of buttons called ChannelButton. The long button press works with the code below.

for button in ChannelButton {
        let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
        button.addGestureRecognizer(longPressGestureRecognizer)
    }
func handleLongPress(sender: UILongPressGestureRecognizer) {
        doSomeFunction(NeedToPassTheButtonsTagHere)
}

But I need it to modify it to be something like this

for button in ChannelButton {
        let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:, button.tag)))
        button.addGestureRecognizer(longPressGestureRecognizer)
    }


    func handleLongPress(sender: UILongPressGestureRecognizer, buttontag) {
        doSomeFunction(buttontag)
}

I know this doesn't work but I'm not sure how to go about it.

Seth Haberman
  • 131
  • 1
  • 13

2 Answers2

1

A UIGestureRecognizer has a view property that is the view that it is attached to. In your case, it will be your button. Use it to get to your button's tag:

func handleLongPress(sender: UILongPressGestureRecognizer) {
    guard let button = sender.view as? UIButton else { return }

    doSomeFunction(button.tag)
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • When I did that, It gave me the yellow warning. . . "Result of call to 'makeSoloRequest' is unused" func handleLongPress(sender: UILongPressGestureRecognizer) { guard let button = sender.view as? UIButton else { return } makeSoloRequest(button.tag) } – Seth Haberman Jun 02 '18 at 08:02
  • What is your makeSoloRequest doing? – Elhoej Jun 02 '18 at 08:03
  • func makeSoloRequest(_ inputNumber: Int) -> NSMutableURLRequest { print("Solo Request!!!") return NSMutableURLRequest(url: URL(string: "\(baserequest)Solo&Input=\(inputNumber)" )!) } – Seth Haberman Jun 02 '18 at 08:03
  • 1
    That function is returning a NSMutableURLRequest, so make sure to point a variable to that function when calling it, for example let url = makeSoloRequest(someInt) – Elhoej Jun 02 '18 at 08:05
  • .. or to ignore the result `_ = makeSoloRequest(button.tag)` – vacawama Jun 02 '18 at 08:08
  • @SethHaberman, did this solve your issue of accessing the button tag in your `UILongPressGestureRecognizer` handler? – vacawama Jun 03 '18 at 15:09
  • Yes! I think this is the code that worked func handleLongPress(sender: UILongPressGestureRecognizer) { guard let button = sender.view as? UIButton else { return }if sender.state == UIGestureRecognizerState.ended { } – Seth Haberman Jun 09 '18 at 04:36
0

You're trying to add an EXTRA parameter (an int) to an IBAction selector. You can't do that. IBActions have one of 3 selectors:

@IBAction func actionNoParams() {
}

@IBAction func actionWSender(_ sender: Any) {
}

@IBAction func actionWSenderAndEvent(_ sender: Any, forEvent event: UIEvent) {
}

One solution would be to look for the location of your long press gesture, and then check what button has that location.

func handleLongPress(sender: UILongPressGestureRecognizer) {

    if sender.state == UIGestureRecognizerState.began {

        let location = sender.location(in: self.view)

        for button in ChannelButton {

            if button.frame.contains(location) {

                //This is the button that is pressed
                //Do stuff
            }
        }
    }
}

Remember to conform to UIGestureRecognizerDelegate

Elhoej
  • 741
  • 7
  • 29