为什么 UILabel TapGesture 只能在标签初始化后起作用?

Why does UILabel TapGesture only work after label initialization?

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

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 代码放在标签中 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