asif saifi
asif saifi

Reputation: 166

UITapGestureRecognizer on UILabel is crashing in swift 3?

Hi i want to add tap gesture on UILabel inside a UITableViewCell . here i have implemented the code like this.

override func awakeFromNib() 
  {
    super.awakeFromNib()
    let tap = UITapGestureRecognizer(target: self, action: 
     #selector(self.tapFunction))
    label.isUserInteractionEnabled = true
    label.addGestureRecognizer(tap)

    }

func tapFunction(sender:UITapGestureRecognizer) {

    print("tap working")
}

but it is giving me error like this

fatal error: unexpectedly found nil while unwrapping an Optional value

Can anyone tell me what is wrong in this?

Upvotes: 1

Views: 313

Answers (2)

asif saifi
asif saifi

Reputation: 166

its working now with the same code . Just i deleted the cell and created a new UITableviewCell. i guess it may be xcode bug

    override func awakeFromNib() 
  {
    super.awakeFromNib()
    let tap = UITapGestureRecognizer(target: self, action: 
     #selector(self.tapFunction))
    label.isUserInteractionEnabled = true
    label.addGestureRecognizer(tap)

    }

func tapFunction(sender:UITapGestureRecognizer) {

    print("tap working")
}

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100533

This should be

 let tap = UITapGestureRecognizer(target: self, action:#selector(self.tapFunction(_:)))

If you use Swift 4 add @objc

@objc func tapFunction(sender:UITapGestureRecognizer) {

  print("tap working")

}

Upvotes: 3

Related Questions