首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swift5 UILabel可点击

swift5 UILabel可点击
EN

Stack Overflow用户
提问于 2022-09-16 08:58:24
回答 1查看 34关注 0票数 1

我正在学习iOS开发。我想让UILabel点击,这是我所做的,但没有结果。

代码语言:javascript
复制
@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

下面是一个函数,当单击label时,该函数将执行一个操作。

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

我为我的英语道歉,我正在使用谷歌翻译。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-16 09:22:43

试试这个:

代码语言:javascript
复制
@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")
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73742386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档