Swift - Multiple UILabel click events to same function

uilabel link detection
uilabel hyperlink objective-c

I need to add a UITapGestureRecognizer to multiple UILabel and they all need to go to the same function for handling.

What I have is this:

@IBOutlet weak var label_something: UILabel!
let tap = UITapGestureRecognizer(target: self, action: #selector(self.myFunction))
label_something.addGestureRecognizer(tap)

Recieved here:

@objc func myFunction(sender:UITapGestureRecognizer) { // Something... }

Working like a charm. Problem is that its only working with one UILabel (If adding addGestureRecognizer(tap) to multiple UIlabel it only works on the last one added)

So my question is:

How to achieve what I want to do here? Five different UILabels with tapRecognizer going to the same function

UIGestureRecognizer is to be used with a single view, you have to create a new instance of UIGestureRecognizer

func setGesture() -> UITapGestureRecognizer {

     var myRecognizer = UITapGestureRecognizer()

     myRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.myFunction))
     return myRecognizer
}

label_something1.addGestureRecognizer(setGesture())    
label_something2.addGestureRecognizer(setGesture())

How to use the same function with multiple buttons, I need to add a UITapGestureRecognizer to multiple UILabel and they all need to go to the same function for handling. What I have is this: Open the Main.storyboard file, then click Library icon in the top right corner to open the UI views library panel. In the library panel, search keyword button and drag the button into Main.storyboard. Do same to add a Label view. 2. Add Button onClick Event Process Method.

If you add new instance from UITapGestureRecognizer its will work fine, for example

let tap = UITapGestureRecognizer(target: self, action: #selector(self.myFunction))
label_1.addGestureRecognizer(tap)
let tap2 = UITapGestureRecognizer(target: self, action: #selector(self.myFunction))
label_2.addGestureRecognizer(tap)

Swift Tutorial: How to perform action when user click on particular , Add the label IBOutlet to the ViewController.swift . Click on "Show the Assistant Editor Button" so that Main.storyboard and ViewController.swift  If the label was multi-line in portrait, and thus the labelsView (the view containing the 2 labels) was expanded to the full width of the screen, on rotation labelsView maintained that same width, instead of adjusting to the new size. I think this is a bug in the system. Fortunately, adding the same constraint in code, worked fine.

Each UIView needs its own recognizer, i have a little helper function for this case. Feel free to copy/paste.

private func addTapRecognizer(toView view: UIView) {
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(didTapButton(sender:)))
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(recognizer)
    }

    @objc private func didTapButton(sender: UIGestureRecognizer) {
        guard let view = sender.view else {
            return
        }

        switch view {
        case self.imageView: // any view you want to check
        default:
            print(sender.debugDescription)
        }
     }

How to Create Multiple Tappable Links in a UILabel, Swift Tutorial: How to perform action when user click on particular text of Creating UITapGesture extension, that contains a function to detect wether user tap on desired text or not Adding UITapGestureRecognizer to UILabel. self. I am an IOS developer having 7+ years of experience in same domain. At the end, you will be able to change : change the background colour of the view. add labels to view, change font, size, alignment and colour. add textfield to the view (similar to login and password field) add button to the view, change button layout and add event on click button.

Clickable UILabel in Swift Programmatically, A good example is a UILabel that looks like the following below the Facebook button. We extend UITapGestureRecognizer to provide a convenient function to detect if a certain range ( NSRange ) is tapped On Tap Event. Every function in Swift has a type, consisting of the function’s parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions.

How To Use One IBAction For Multiple Buttons In Swift, To make UILabel clickable in Swift, you will need to create a Gesture Recognizer which will call a custom function once user taps on a label. Use the line Break Mode property to control how the label splits the text into multiple lines, and the truncation behavior associated with the final line. Use Auto Layout to position and optionally size the label. The intrinsic content size for a label defaults to the size that displays the entirety of the content on a single line.

Coordinating Multiple Gesture Recognizers, Use One IBAction Function To Process Multiple Button's Click Event Demo. Open Xcode UI component Library and add one label and two buttons in ios swift xcode connect the second button to the same ibaction function  Search for “UILabel” in the Library pane in the lower right hand corner and drag the UILabel onto your view. Next, repeat the same steps but this time, search for “UITextField” and drag and drop that element onto your view. Next, repeat the same steps but this time, search for “UIButton” and drag and drop that element onto your view.

Comments
  • How did you determine myFunction(sender:) called only for one label? If you tap on the other label, It didn't call. Did you check with log?
  • Yes, a simple print("hepp") in the function only showed when clicking the one that worked.
  • Could you see here some more code? ie how you assign the label_something?
  • Why are you using label instead of a buttons?
  • @Sourcey86 Where did you add gesture for all other labels?
  • Are you sure, need to create new gesture for every label?
  • @Mani A gesture recognizer can only be attached to one view. The reason is because each recognizer can have it's own gesture state (one is being triggered and the others are untouched).
  • i think yes, because if you use the same instance, it will be added for the last label.
  • @Crazyrems ya got your point. after seeing this. stackoverflow.com/questions/4747238/…
  • @Mani yes. Each label needs to get a tap gesture recognizer, i.e one UITapGestureRecognizer can be only assigned to single component, that's why the last label recognize the tap, it would automatically removed from the previous labels. However, all gestures can have the same selector method.