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.
A gesture recognizer decouples the logic for recognizing a sequence of touches (or other input) and acting on that recognition.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With