gpt4 book ai didi

ios - 添加到 UILabel 的点击手势识别器不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 08:19:54 26 4
gpt4 key购买 nike

我使用以下代码将手势识别器添加到 UILabel。 Storyboard中的标签已启用用户交互,但当我点击标签时,不会调用 onUserClickingSendToken 方法。

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBOutlet weak var tokenDisplay: UILabel!
var tapGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target:self, action: #selector(onUserClickingSendToken(_:)))

override func viewDidLoad() {
super.viewDidLoad()
tapGestureRecognizer.numberOfTapsRequired = 1
tokenDisplay.addGestureRecognizer(tapGestureRecognizer)
}

func onUserClickingSendToken(_ sender: Any)
{
....

最佳答案

初始化 viewDidLoad 中的 tapRecognizer 应该可以做到,因为您在初始化 View 之前以自身为目标

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBOutlet weak var tokenDisplay: UILabel!
var tapGestureRecognizer:UITapGestureRecognizer!

override func viewDidLoad() {
super.viewDidLoad()
tapGestureRecognizer = UITapGestureRecognizer(target:self, action: #selector(onUserClickingSendToken(_:)))
tapGestureRecognizer.numberOfTapsRequired = 1
tokenDisplay.isUserInteractionEnabled = true
tokenDisplay.addGestureRecognizer(tapGestureRecognizer)
}

@objc func onUserClickingSendToken(_ sender: Any)
{
....

关于ios - 添加到 UILabel 的点击手势识别器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41772783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com