1

I searched through all the questions for this topic here on stack overflow, but I can't seem to figure it out, since I seem to do everything right, still I get this error. I'm trying to implement a recording button for my chat, which records as long as the button is pressed. I always get the following error: [HenrysApp.ChatViewController longPress:]: unrecognized selector sent to instance 0x7f952602dc00

Here is the code:

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
    longPressGestureRecognizer.minimumPressDuration = 1


    self.recordingSession = AVAudioSession.sharedInstance()

    do {
        try self.recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try self.recordingSession.setActive(true)
        self.recordingSession.requestRecordPermission() { [unowned self] allowed in
            DispatchQueue.main.async {
                if allowed {
                    self.record_button.addGestureRecognizer(longPressGestureRecognizer)
                } else {
                    // failed to record!
                }
            }
        }
    } catch {
        // failed to record!
    }


 // Gesture Recognizer for the Record Button, so as long as it is pressed, record!
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer){
    if longPressGestureRecognizer.state == .began {
        print("long press began")
        let recordingTapImage = UIImage(named: "ic_mic_none_white")
        record_button.setImage(recordingTapImage, for: .normal)
        self.recording()
    }
    if longPressGestureRecognizer.state == .ended {
        print("long press ended")
        let recordImage = UIImage(named: "ic_mic_white")
        record_button.setImage(recordImage, for: .normal)
        self.recordTapRelease()
    }
}
  • What class is `func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer)` declared in? – Frankie Nov 14 '16 at 13:52

3 Answers3

0

Try first t replace "longPress:" with #selector(longPress(_:)) ( first line ), which is the new naming convention

CZ54
  • 5,488
  • 1
  • 24
  • 39
0

You need to change

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")

with this

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
Rajat
  • 10,977
  • 3
  • 38
  • 55
0
  • It might be because the outlet may not connected properly. Click the view controller or any UI element in storyboards, and open connections inspector on the right. There you may find if any UI element is properly connected or not.
rajtharan-g
  • 432
  • 5
  • 14