我的代码:
override func viewDidLoad() {
super.viewDidLoad()
//Adding the long gesture
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
//Adding the gesture to the button
hydrogenBoxButton.addGestureRecognizer(longGesture)
//Adding a tag for the button
hydrogenBoxButton.tag = 1
}
然后我的函数在选择器中被调用:
@objc func longTap(sender: UIButton) {
if sender.tag == 1 {
print("Hydrogen long tap worked!")
} else if sender.tag == 2 {
print("Helium long tap worked!")
}
}
但是,当我运行此命令并尝试长按按钮时,我收到此错误:
-[UILongPressGestureRecognizer tag]: unrecognized selector sent to instance 0x7fbc5ce00680
我不知道是因为我命名标签错误还是因为我创建 LongPressGestureRecognizer 的方式有问题。
最佳答案
参数的类型为UILongPressGestureRecognizer
@objc func longTap(sender: UILongPressGestureRecognizer ) {
let tag = sender.view!.tag
if tag == 1 {
print("Hydrogen long tap worked!")
} else if tag == 2 {
print("Helium long tap worked!")
}
}
关于swift - 尝试调用长按手势并运行命令,但在函数中使用选择器时遇到 SIGABRT 错误。什么没有连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59569378/