Почему UILabel TapGesture работает только после инициализации метки? - PullRequest
1 голос
/ 06 февраля 2020

У меня есть следующий код для инициализации метки:

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? Или что-то еще?

1 Ответ

1 голос
/ 06 февраля 2020

Код tapGesture в следующем коде добавляется внутри инициализатора Closure, и вы назначаете цель как self, что недопустимо. Coz 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, когда происходит жест

...