12

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?

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
BadmintonCat
  • 9,416
  • 14
  • 78
  • 129

6 Answers6

24

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.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
5

Swift 4 using #selector.

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

@objc func didSelectItem(sender: AnyObject?) {
    print("didSelectItem: \(sender)")
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
2

Also try adding a parameter to your method:

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

func yourMethodName(sender: AnyObject?)
{
    println("Clicked yourMethodName")
}
ODAXY
  • 335
  • 4
  • 12
1

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!

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
1

for swift 4

override func viewDidLoad() {
        super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(SignUpViewController.click))
    userImage.addGestureRecognizer(tap)
    userImage.isUserInteractionEnabled = true
}

@objc func click()
{
    print("Tapped on Image")
}

where SignUpViewController is your viewcontroller name

roshan posakya
  • 1,010
  • 10
  • 14
0

In my case, with Swift 4, the selector was correctly defined, but since the item with the gesture was inside a collection view, it was causing the selector not to be found. I implemented a custom cell and configured the selector inside of it

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "recent", for: indexPath) as! MyCollectionViewCell

cell.configureGestureWith(entry: recentEntries.sorted(by: {$0.createdAt > $1.createdAt})[indexPath.row], sender: self)

Hope this helps some one.

Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75