Search code examples
iosswiftuilabeluitapgesturerecognizerclickable

UITapGestureRecognize with UILabel get the labels text in function


I have a Label for a phone number and an email. If you click on the email label, the mail app should open with the mail defined inside the label.

That is my function:

func tapMailFunction(sender:UITapGestureRecognizer) {
    print("tapMail working")
}

Inside the cellForRowAt i set the following:

cell.Mail.addGestureRecognizer(tapMail)

That is how my UITapGestureRecognizer looks like:

let tapMail = UITapGestureRecognizer(target: self, action: Selector(TestViewController.tapMailFunction)) 

How can get the cell.Mail.text inside my function?


Solution

  • You can use the recognizer object to get the related view. Something like this:

    func tapMailFunction(sender:UITapGestureRecognizer) {
        if let label = sender.view as? UILabel {
            let text = label.text
        }
    }