Swift 4无法识别的选择器设置手势识别器

时间:2018-05-26 15:39:42

标签: ios swift

我正在为表格视图单元格设置UILongPressGestureRecognizer,并且在尝试测试手势时,我在运行时遇到以下异常:

2018-05-26 10:10:21.740077-0500 Rumble Ios[43135:916767] -[UIButton longPress]: unrecognized selector sent to instance 0x7fea34e210c0
2018-05-26 10:10:21.756166-0500 Rumble Ios[43135:916767] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton longPress]: unrecognized selector sent to instance 0x7fea34e210c0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a76e1e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x0000000109e03031 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010a7ef784 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   UIKit                               0x000000010ae1973b -[UIResponder doesNotRecognizeSelector:] + 295
    4   CoreFoundation                      0x000000010a6f0898 ___forwarding___ + 1432
    5   CoreFoundation                      0x000000010a6f0278 _CF_forwarding_prep_0 + 120
    6   UIKit                               0x000000010b1e341b -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
    7   UIKit                               0x000000010b1ec1f0 _UIGestureRecognizerSendTargetActions + 109
    8   UIKit                               0x000000010b1e9a38 _UIGestureRecognizerSendActions + 307
    9   UIKit                               0x000000010b1e8c8c -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 859
    10  UIKit                               0x000000010b1cdcf0 _UIGestureEnvironmentUpdate + 1329
    11  CoreFoundation                      0x000000010a710607 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    12  CoreFoundation                      0x000000010a71055e __CFRunLoopDoObservers + 430
    13  CoreFoundation                      0x000000010a6f4b81 __CFRunLoopRun + 1537
    14  CoreFoundation                      0x000000010a6f430b CFRunLoopRunSpecific + 635
    15  GraphicsServices                    0x0000000112bb0a73 GSEventRunModal + 62
    16  UIKit                               0x000000010abeb0b7 UIApplicationMain + 159
    17  Rumble Ios                          0x000000010924b777 main + 55
    18  libdyld.dylib                       0x000000010ebd9955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

CustomTableViewCell.swift中的代码:

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var selectButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()

        let longPressGesture = UILongPressGestureRecognizer(target: selectButton, action: #selector(longPress))
        longPressGesture.minimumPressDuration = 1
        self.addGestureRecognizer(longPressGesture)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @objc func longPress() {
        print("longPress")
    }
}

我一直无法通过谷歌来回答这个问题。我读过的所有内容都表明这些错误是由错别字引起的(应该在构建时捕获)。我还没有找到任何我遇到的问题。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

您应该向target参数提供自我,并为您的selectButton添加识别器。

更新代码:

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1
selectButton.addGestureRecognizer(longPressGesture)

答案 1 :(得分:1)

变化:

let longPressGesture = UILongPressGestureRecognizer(target: selectButton, action: #selector(longPress))

为:

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))

目标需要是实现选择器的类。在这种情况下,这是您的单元格,而不是按钮。