Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer sender is the gesture, not the ui object

I've got a button called and I gave it a UIGestureRecognizer so that an IBAction is only run when the button is long pressed. You do this by adding a UILongPressGestureRecognizer to the button iteself. Then you control drag that gesture recognizer to a function like this:

 @IBAction func handleGesture(sender: UIGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Began
    {
        let labelTap1=UITapGestureRecognizer(target: self, action: "labelTapped:")
        let alertController = UIAlertController(title: "**I want this to be the title of the button**", message:
            "This is the description of the function you pressed", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

As you can see the UIAlertController appears, I want the title to show the buttons current title. If the sender of this function was the button, I could call this with sender.currentTitle. How can I get the title of the button that is tied to the gesture, when the sender of the function is the gesture.

I have seen posts like this in objective C, but nothing really on Swift. This is all for a project to get a buttons description when you longpress on the button itself.

like image 375
modesitt Avatar asked Jun 22 '15 04:06

modesitt


People also ask

What is gesture recognizer?

A gesture recognizer decouples the logic for recognizing a sequence of touches (or other input) and acting on that recognition.

How to Add tap gesture recognizer to UIView Swift?

Adding a Tap Gesture Recognizer to an Image View in Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the image view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

Which of the following is an Apple provided base class for recognizing multitouch input gestures?

The UIResponder class provides methods to recognize and handle the mouse events that occur when the user taps or drags on the iPhone's screen.


1 Answers

You can get a reference to the view the gesture is added to via its view property. In this case you are adding it to the button so the view property would return you you the button.

let button = sender.view as? UIButton
like image 151
Kris Gellci Avatar answered Nov 02 '22 17:11

Kris Gellci