ios - 如何在 Swift 3 中制作可点击的 UILabel?

标签 ios swift uilabel

我正在尝试按照以下代码制作可点击的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/

相关文章:

ios - Swift:将图像设置为自定义单元格ImageView

iphone - ios TreeView 组件的数据模型

ios - 如何使用 NSPredicate 在 CoreData 中查找标题

ios - objective-c : Resizing a UIImageView

arrays - 实体数组不会 swift 发生变异?

Swift 3 - 更新后如何添加导航 Controller 按钮(如返回) - 刷新并重新加载我的 View Controller

swift - 尝试在 SwiftUI 中使用时,实例方法 'appendInterpolation' 要求 'Decimal' 符合 '_FormatSpecifiable'

swift - 将多个 pickerView 选定值导出到 UILabel 上的数字

ios - 如何创建具有最大宽度的多行 UILabel?

ios - 将 UILabel.attributedText 设置为相同的值很昂贵吗?