Reputation: 335
I have a small issue that I couldn't fix for a few hours now! I have a simple ViewController with a label, which is linked to my ViewController class. I also set up an UIGestureRecognizer to change the text of the label when the label is clicked. But weirdly nothing happens at all.
The text of the label changes from whatever is set in the Storyboard to "Hello", so the setup is correct. But when I click the label, nothing happens.
Here's the entire ViewController class:
import UIKit
class PremiumViewController: UIViewController {
// Views
@IBOutlet weak var premiumLabel: UILabel!
// View Did Load
override func viewDidLoad() {
super.viewDidLoad()
premiumLabel.text = "Hello"
// Tap listener
let tap = UIGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
premiumLabel.isUserInteractionEnabled = true
premiumLabel.addGestureRecognizer(tap)
}
@objc func clicked() {
premiumLabel.text = "You clicked me"
}
}
Upvotes: 0
Views: 48
Reputation: 456
Use UITapGestureRecognizer
instead of UIGestureRecognizer
change from
let tap = UIGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
to
let tap = UITapGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
Upvotes: 4