简体   繁体   English

swift5 UILabel 可点击

[英]swift5 UILabel Clickable

I am learning iOS development.我正在学习iOS开发。 And I want to make UILabel clickable Here is what I have done but with no result.我想让 UILabel 可点击这是我所做的,但没有结果。

@IBOutlet weak var coordinate: UILabel!
override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        coordinate.adjustsFontSizeToFitWidth = true
        
        let tap = UIGestureRecognizer(target: self, action: #selector(uiLabelClicked()))
        coordinate.addGestureRecognizer(tap)
        coordinate.isUserInteractionEnabled = true

Below is a function that is expected to perform an action when label is clicked.下面是一个 function,当点击 label 时,它会执行一个动作。

 @objc func uiLabelClicked(){
            let alert = UIAlertController(title: "Coordinate", message: "I have been clicked...", preferredStyle: .alert)
            self.present(alert, animated: true, completion: nil)
            debugPrint("clicked")
            
        }

I apologize for my English I am using google translator.我为我的英语道歉,我正在使用谷歌翻译。

Try this:尝试这个:

@IBOutlet weak var coordinate: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    coordinate.adjustsFontSizeToFitWidth = true

    let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelAction(_:)))
    coordinate.addGestureRecognizer(tap)
    coordinate.isUserInteractionEnabled = true
}

@objc func labelAction(_ sender: UIGestureRecognizer) {
    let alert = UIAlertController(title: "Coordinate", message: "I have been clicked...", preferredStyle: .alert)
    self.present(alert, animated: true, completion: nil)
    debugPrint("clicked")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM