swift - 尝试调用长按手势并运行命令,但在函数中使用选择器时遇到 SIGABRT 错误。什么没有连接?

标签 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.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/

相关文章:

generics - 重载泛型方法的重载解决问题

ios - 带有 UIButton 的自定义 UITableView 单元不调用 didSelectRowAtIndexPath

objective-c - 单击时更改按钮背景颜色

ios - return语句永远不会被执行警告

iphone - 通过 UIButton 将文本从 UITextView 复制到剪贴板(粘贴板)?

Swift:NSNumber 不是 Float 的子类型

ios - cocoa 荚 : Unable to remove the Pod from project

ios - 如何在 Firebase 中退出应用程序后保持用户 session

ios - 如何解决导航限制警告?

iphone - plist 不会以编程方式删除