[英]How can I make a clickable UILabel in Swift 3?
我正在嘗試按照以下代碼制作可點擊的UILabel
:
class ViewNotificationsDetails: UIViewController {
@IBOutlet weak var back: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewNotificationsDetails.tapFunction))
back.isUserInteractionEnabled = true
back.addGestureRecognizer(tap)
}
@objc func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
}
但是在執行代碼時,我收到錯誤 ->
線程 1:致命錯誤:在“back.isUserInteractionEnabled = true”行上解開 Optional 值時意外發現 nil。
可能是什么問題呢?
試試這個代碼,對我來說效果很好
class ViewController: UIViewController {
@IBOutlet weak var cliclableLable: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
cliclableLable.isUserInteractionEnabled = true
cliclableLable.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
}
也不要忘記將您的標簽與代碼鏈接
使用back.userInteractionEnabled = true
你做錯了 isUserInteractionEnabled。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.