gpt4 book ai didi

ios - UILabel 设置 onClick

转载 作者:行者123 更新时间:2023-11-29 05:42:05 40 4
gpt4 key购买 nike

我正在 Swift 3 中构建一个应用程序,因此我想在单击特定 UILabel 时调用一个函数,因此我编写了以下代码,但不起作用:

let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)

如何使 UILabel 可点击?

最佳答案

设置为 UILabel 启用用户交互,并在 viewDidLoad() 中添加以下代码

self.label.isUserInteractionEnabled = true

let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped))
self.label.addGestureRecognizer(tap)

添加点击操作功能如下:

@objc func labelTapped(_ gestureRecognizer: UITapGestureRecognizer) {
print("Label clicked")
}

请让用户确保 View 中没有其他透明 View 与 UILabel 重叠。如果 UILabel 是另一个 View 的一部分,请确保容器 View 的用户交互已启用。

希望这有帮助。

关于ios - UILabel 设置 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56458140/

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