簡體   English   中英

swift5 UILabel 可點擊

[英]swift5 UILabel Clickable

我正在學習iOS開發。 我想讓 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

下面是一個 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")
            
        }

我為我的英語道歉,我正在使用谷歌翻譯。

嘗試這個:

@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