Swift 尝试调用长点击手势并运行命令,但函数中的选择器出现SIGABRT错误。什么是';没有连接?
我的代码:Swift 尝试调用长点击手势并运行命令,但函数中的选择器出现SIGABRT错误。什么是';没有连接?,swift,xcode,uibutton,tags,uilongpressgesturerecogni,Swift,Xcode,Uibutton,Tags,Uilongpressgesturerecogni,我的代码: override func viewDidLoad() { super.viewDidLoad() //Adding the long gesture let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap)) //Adding the gesture to the button hydrogenBoxButton.addG
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!")
}
}
但是,当我运行此操作并尝试长按按钮时,我会出现以下错误:
-[UILongPressGestureRecognitor tag]:发送到实例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!")
}
}
谢谢,我不再有任何错误了。但是,现在长按将不起作用。您是否同时使用Tap手势和LongTap手势?如果没有,请尝试添加LongPressPicture.minimumPressDuration=1.0并检查它是否工作?是否工作?是的,正在工作,谢谢