Swift UIGestureRecognizer 不工作/不做任何事
Swift UIGestureRecognizer is not working / is not doing anything
我有一个小问题,我现在几个小时都无法解决!我有一个带有标签的简单 ViewController,它链接到我的 ViewController class。我还设置了一个 UIGestureRecognizer 以在单击标签时更改标签的文本。但奇怪的是什么都没有发生。
标签的文本从 Storyboard 中设置的任何内容更改为“Hello”,因此设置是正确的。但是当我点击标签时,没有任何反应。
这是整个 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"
}
}
使用UITapGestureRecognizer
代替UIGestureRecognizer
变化自
let tap = UIGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
到
let tap = UITapGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
我有一个小问题,我现在几个小时都无法解决!我有一个带有标签的简单 ViewController,它链接到我的 ViewController class。我还设置了一个 UIGestureRecognizer 以在单击标签时更改标签的文本。但奇怪的是什么都没有发生。
标签的文本从 Storyboard 中设置的任何内容更改为“Hello”,因此设置是正确的。但是当我点击标签时,没有任何反应。
这是整个 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"
}
}
使用UITapGestureRecognizer
代替UIGestureRecognizer
变化自
let tap = UIGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
到
let tap = UITapGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))