UITapGestureRecognize 和 UILabel 在函数中获取标签文本

UITapGestureRecognize with UILabel get the labels text in function

我有一个 phone 号码和电子邮件的标签。如果您单击电子邮件标签,邮件应用程序应打开并显示标签内定义的邮件。

这是我的职能:

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

在 cellForRowAt 中,我设置了以下内容:

cell.Mail.addGestureRecognizer(tapMail)

这就是我的 UITapGestureRecognizer 的样子:

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

如何在我的函数中获取 cell.Mail.text?

您可以使用识别器对象来获取相关视图。像这样:

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