ios - 为什么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 无法识别的界限?或者是其他东西?
解决方案
以下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
手势发生的时间
推荐阅读
- sql - 使用 WHEN 语句检查 SQL 值时出现问题
- c# - ASP.NET Core 迁移到 Core 3.0:迁移警告 CS0618 'RazorViewAttribute' 已过时
- tensorflow - 一个奇怪的错误-----InvalidArgumentError: slice index -1 of dimension 0 out of bounds.()
- python - 如何将值从一个类返回到另一个类?
- python - 消费者:无法连接到 amqp://dfmngfek:**@salamander.rmq.cloudamqp.com:5672/dfmngfek:[Errno 104] 连接被对等方重置
- swift - 如何在 Catalyst 中使用 NSSharingService?
- php - 通过 PHP 从 CSV 文件中删除特定行
- reactjs - 构建 Ionic 4 启动画面,没有做任何事情(不起作用)
- java - 配置级别以下的 Drools 日志记录
- batch-file - 使用 CMD 更新 xml 节点