我正在尝试按照以下代码制作可点击的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")
}
}
但是在执行代码时,我收到错误 ->
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on the line "back.isUserInteractionEnabled = true".
可能是什么问题?
最佳答案
试试这个代码,对我来说效果很好
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")
}
}
也不要忘记将您的标签与代码链接
关于ios - 如何在 Swift 3 中制作可点击的 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47651589/