如何将 LongPressGestureRecognizer 添加到禁用的 TextField

How to add LongPressGestureRecognizer into disabled TextField

我有 isEnabled = false

的 TextField

现在我正在尝试添加 UILongPressGestureRecognizer

在 UITableViewCell 中:

override func awakeFromNib() {
    super.awakeFromNib()
    let tap = UILongPressGestureRecognizer(target: userNameTextField, action: #selector(userNamelongPressAction))
    self.addGestureRecognizer(tap)
}

但我崩溃了

'NSInvalidArgumentException', reason: '-[UITextField userNamelongPressAction]: unrecognized selector sent to instance

我能做什么? 谢谢

这是一个常见的错误,您错误地将目标添加到 UITextField,而必须将目标设置为实现方法的位置 userNamelongPressAction

此代码假设您已在此 class 上下文

中实施 userNamelongPressAction 方法
override func awakeFromNib() {
    super.awakeFromNib()
    let tap = UILongPressGestureRecognizer(target: self, action: #selector(userNamelongPressAction))
    self.addGestureRecognizer(tap)
}

希望对您有所帮助