首页 > 解决方案 > 为什么UILabel TapGesture 只能在标签初始化后工作?

问题描述

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

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 无法识别的界限?或者是其他东西?

标签: iosswiftuilabelswift4uitapgesturerecognizer

解决方案


以下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正如预期的那样,您将目标设置selfViewController有效的。

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手势发生的时间


推荐阅读