gpt4 book ai didi

ios - 为什么UILabel TapGesture 只能在标签初始化后工作?

转载 作者:行者123 更新时间:2023-12-01 22:02:43 24 4
gpt4 key购买 nike

我有以下代码来初始化标签:

let forgotPasswordLabel: UILabel = {
let label = UILabel()
label.text = "Forgot password?"
label.font = ApplicationScheme.instance.containerScheme.typographyScheme.subtitle1
label.textColor = ApplicationScheme.instance.containerScheme.colorScheme.onPrimaryColor
label.textAlignment = .right
label.sizeToFit()
label.accessibilityRespondsToUserInteraction = true
label.isUserInteractionEnabled = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

后来我将它添加到 subview 中:

let passwordLabel = forgotPasswordLabel
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onForgotPasswordClick))
passwordLabel.addGestureRecognizer(tapGesture)
self.view.addSubview(passwordLabel)

如果我这样做代码,那么它工作正常,但是如果我将 tapGesture 代码放在标签 forgotPasswordLabel 初始化中,则不会触发点击手势。这是为什么?

不起作用的代码:

let forgotPasswordLabel: UILabel = {
let label = UILabel()
label.text = "Forgot password?"
label.font = ApplicationScheme.instance.containerScheme.typographyScheme.subtitle1
label.textColor = ApplicationScheme.instance.containerScheme.colorScheme.onPrimaryColor
label.textAlignment = .right
label.sizeToFit()
label.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onForgotPasswordClick))
label.addGestureRecognizer(tapGesture)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

这与垃圾收集器有关吗? UILabel 无法识别的界限?或者是其他东西?

最佳答案

tapGesture在下面的代码中添加了 Closure初始化器,然后将目标分配为 self这是无效的。因为 self这里不是 ViewController

let forgotPasswordLabel: UILabel = {
let label = UILabel()
label.text = "Forgot password?"
label.font = ApplicationScheme.instance.containerScheme.typographyScheme.subtitle1
label.textColor = ApplicationScheme.instance.containerScheme.colorScheme.onPrimaryColor
label.textAlignment = .right
label.sizeToFit()
label.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onForgotPasswordClick))
label.addGestureRecognizer(tapGesture)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

在下面的代码中, tapGesture正在按预期工作,您将目标设置为 self这是 ViewController这是有效的。
let forgotPasswordLabel: UILabel = {
let label = UILabel()
label.text = "Forgot password?"
label.font = ApplicationScheme.instance.containerScheme.typographyScheme.subtitle1
label.textColor = ApplicationScheme.instance.containerScheme.colorScheme.onPrimaryColor
label.textAlignment = .right
label.sizeToFit()
label.accessibilityRespondsToUserInteraction = true
label.isUserInteractionEnabled = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

let passwordLabel = forgotPasswordLabel
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onForgotPasswordClick))
passwordLabel.addGestureRecognizer(tapGesture)
self.view.addSubview(passwordLabel)

如果是 tapGesture当您设置目标时,它会告诉您在哪里寻找 action当手势发生时

关于ios - 为什么UILabel TapGesture 只能在标签初始化后工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60087189/

24 4 0