o037
o037

Reputation: 137

Swift 4 Unrecognized Selector setting up a gesture recognizer

I'm working on setting up a UILongPressGestureRecognizer for table view cells and I'm getting the following exception at runtime when trying to test the gesture:

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

Code in 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")
    }
}

I've been unable to google my way to an answer on this one. Everything I've read suggests that these kinds of errors are caused by typos (that should be caught at buildtime). I haven't been able to find any problems like the ones I'm experiencing.

Any help would be much appreciated!

Upvotes: 0

Views: 880

Answers (2)

Ivan Smetanin
Ivan Smetanin

Reputation: 2039

You should provide self to target parameter and add recognizer to your selectButton.

Updated code:

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

Upvotes: 2

rmaddy
rmaddy

Reputation: 318955

Change:

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

to:

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

The target needs to be the class that implements the selector. In this case that is your cell, not the button.

Upvotes: 1

Related Questions