Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer unrecognized selector sent to instance

I've searched for solutions to this problem but couldn't find anything that seems to address it in my case. I'm getting the above exception from a UITapGestureRecognizer.

Here's the simplified code:

import UIKit;

class ViewController : UIViewController, UIScrollViewDelegate
{
    @IBOutlet weak var scrollView:UIScrollView!;
    var imageView:UIImageView!;

    override func viewDidLoad()
    {
        super.viewDidLoad();

        ... set up imageView/scrollView here ...

        let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "onScrollViewDoubleTapped");
        doubleTapRecognizer.numberOfTapsRequired = 2;
        doubleTapRecognizer.numberOfTouchesRequired = 1;
        scrollView.addGestureRecognizer(doubleTapRecognizer);
    }


    func onScrollViewDoubleTapped(recognizer:UITapGestureRecognizer)
    {
    }
}

Can anyone tell what is wrong with this code? It seems all correct to me. I suspect that it has to do with assigning ViewController as delegate to scrollView (or vice versa)? However the ViewController is set as the delegate to scrollView. But maybe it's something else that causes this error?

like image 776
BadmintonCat Avatar asked Jul 29 '14 17:07

BadmintonCat


4 Answers

Try adding a colon to your selector string.

let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "onScrollViewDoubleTapped:");

As cabellicar123 mentioned, this indicates that the selector takes an argument.

like image 112
Connor Avatar answered Nov 01 '22 04:11

Connor


Swift 4 using #selector.

let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSelectItem(sender:)))

@objc func didSelectItem(sender: AnyObject?) {
    print("didSelectItem: \(sender)")
}
like image 42
Daniel Storm Avatar answered Nov 01 '22 04:11

Daniel Storm


Also try adding a parameter to your method:

...(target: self, action: "yourMethodName:")

func yourMethodName(sender: AnyObject?)
{
    println("Clicked yourMethodName")
}
like image 2
ODAXY Avatar answered Nov 01 '22 04:11

ODAXY


Maybe could help someone: I had this error because I declared private the selector method:

func setup() {
  let singleFingerTap = UITapGestureRecognizer(target: self, action: "didTapOnViewInteraction:")
  singleFingerTap.numberOfTapsRequired = 1
  self.viewInteraction.addGestureRecognizer(singleFingerTap)
}

private func didTapOnViewInteraction(recognizer: UITapGestureRecognizer) {

}

Removing "private" keyword, all works great!

like image 1
Luca Davanzo Avatar answered Nov 01 '22 05:11

Luca Davanzo