gpt4 book ai didi

ios - 如何在标签中设置 Action ?

转载 作者:行者123 更新时间:2023-11-28 13:36:28 25 4
gpt4 key购买 nike

这是我的代码:

在 viewDidLoad 中添加手势

let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
toastLabel.addGestureRecognizer(tap)
toastLabel.isUserInteractionEnabled = true

func showLongToast( message: String) {
toastLabel = UILabel(frame: CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125))
toastLabel.numberOfLines = 0
toastLabel.textAlignment = .center
toastLabel.contentMode = .center
toastLabel.backgroundColor = UIColor.white.withAlphaComponent(0.7)
toastLabel.textColor = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)
toastLabel.font = UIFont(name: "Montserrat-Medium", size: 15.0)
let trimmedString = message.trimmingCharacters(in: .whitespacesAndNewlines)
let string = NSMutableAttributedString(string: trimmedString)
string.setColorForText("Enter Manually", with: #colorLiteral(red: 1, green: 0.4196078431, blue: 0.1812392979, alpha: 1))
toastLabel.attributedText = string
toastLabel.layer.cornerRadius = 25
toastLabel.clipsToBounds = true
controller.view.addSubview(toastLabel)
controller.view.bringSubviewToFront(toastLabel)
}

我已经从 viewController 调用了这个函数:

showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)

但是 toast 消息不能设置 Action 了?有什么想法请发表评论。谢谢。

最佳答案

这就是你的 viewDidLoad()tapLabel(tap:) 的样子,

override func viewDidLoad() {
super.viewDidLoad()
self.showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
toastLabel.addGestureRecognizer(tap)
toastLabel.isUserInteractionEnabled = true
}

@objc func tapLabel(tap: UITapGestureRecognizer) {
print("tapped..!!!")
}

并且 showLongToast 的签名不会与您的代码一起编译。应该是这样的,

func showLongToast( message: String, controller: UIViewController) {
//your code here...
}

enter image description here

关于ios - 如何在标签中设置 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56561611/

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