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

标签 ios swift uilabel swift4 uitapgesturerecognizer

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

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/

相关文章:

swift - Vapor:处理 HTTPS 请求?

ios - 为什么清空 NavigationStack 路径后对象仍在内存中?

ios - 以编程方式使用iOS无障碍声音

ios - 为什么我通过 NSURLConnection 收到 "connectionFailed unrecognized selector"?

iOS - 使用自动布局设置 UILabel 的最大宽度

swift - 在 Swift 中,您在哪里声明与 addGestureRecognizer() 相关的 UITapGestureRecognizer()?

ios - UILabel 没有在 UITableViewCell 中断行

ios - Realm 查询属性(property)总和

swift - 如何写入 NSPasteboard 并使用相同的指针获取它

Swift:从父类(super class)返回子类类型的对象数组