Swift3 iOS - How to make UITapGestureRecognizer trigger function

75,894

Solution 1

From your code you are using swift 3.0 so change your selector syntax like this

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))

and Your function like this

func tapBlurButton(_ sender: UITapGestureRecognizer) {
    print("Please Help!")
}

Edit:

Not idea that you are using button with tap gesture, instead of that use inbuilt method addTarget for button no need to create tap gesture for it like this

qsBlurButton.addTarget(self, action: #selector(self.tapBlurButton(_:)), forControlEvents: .TouchUpInside)

func tapBlurButton(_ sender: UIButton) {
    print("Please Help!")
}

Solution 2

Swift 3

In the case where the func for the selector looks like this (note: it doesn't have a _):

func tapBlurButton(sender: UITapGestureRecognizer) {
  print("Please Help!")
}

Then the selector looks like this:

#selector(self.tapBlurButton(sender:))

So the final code for the selector is this:

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

If you do not specify that the first parameter has _, then you need to use the full name of the first parameter.

Solution 3

To add a gesture recognizer you have to create one indicating which function it will call:

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapFrom(recognizer:)))

Then add it to the element you need. (You are using a button, and as others have explained buttons have their native 'addTarget' methods)

For explanation purposes imagine you want to add it to an UIView:

self.someView.addGestureRecognizer(tapGestureRecognizer)

And don't forget that some elements are "not user interactive" by default so you might have to change that property too:

self.someView.isUserInteractionEnabled = true

In Swift 4 the function needs the @objc declaration:

@objc func handleTapFrom(recognizer : UITapGestureRecognizer)
{
    // Some code...
}
Share:
75,894
iSebbeYT
Author by

iSebbeYT

Updated on October 25, 2020

Comments

  • iSebbeYT
    iSebbeYT over 3 years

    I am trying to add a UITapGesture to a UIButton so it will trigger a function when tapped. I am using Swift 3 and is getting some error:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwiftRunner.ViewController tapBlurButton]: unrecognized selector sent to instance 0x149e07610'

    This is roughly what I have:

    // Swift 3
    import UIKit
    class ViewController {
    
       @IBOutlet weak var qsBlurButton: UIButton!       
    
       override func viewDidLoad() {
          super.viewDidLoad()
    
          let tapGesture = UITapGestureRecognizer(target: self, action: Selector(("tapBlurButton")))
          qsBlurButton.addGestureRecognizer(tapGesture)
       }
    
       func tapBlurButton(sender: UITapGestureRecognizer) {
          print("Please Help!")
       }
    }
    
  • iSebbeYT
    iSebbeYT over 7 years
    Ok, I tried that but I am still getting the same error. Also getting the warning "No method declared with Objective-C selector 'tapBlurButton:'"
  • oyvindhauge
    oyvindhauge over 7 years
    @Sebbe you need to put the @objc attribute on your selector method. @objc func tapBlurButton() ....
  • Nirav D
    Nirav D over 7 years
    Try to change your function declaration with my one.
  • iSebbeYT
    iSebbeYT over 7 years
    Now I got it working! The edit version was the correct solution! Ty!
  • Nirav D
    Nirav D over 7 years
    Welcome mate :) Happy Coding :)
  • e.w. parris
    e.w. parris over 7 years
    Thanks, I've been looking for the new Swift 3.0 selector syntax for two days.
  • aircraft
    aircraft over 7 years
    @Nirav D Hi,bro, how can you get 17.9k use only 6 monthes?
  • 1QuickQuestion
    1QuickQuestion about 7 years
    @NiravD Thanks! I was just trying to get this to work with Swift 3!
  • Nirav D
    Nirav D almost 7 years
    @RKosamia Does i know you ? What you want to send ?
  • RKosamia
    RKosamia almost 7 years
    @NiravD Have some questions , iOS related so wanted to discuss through email.
  • Nirav D
    Nirav D almost 7 years
    @RKosamia Ask question here on SO and give me link here in comment, if I have solution will post one
  • khunshan
    khunshan over 6 years
    In Swift 4 the function needs the @objc declaration. Why?
  • Pochi
    Pochi over 6 years
    Because in Swift 3 it was "inferred" automatically. However in Swift 4 its not anymore. The @objc is used to expose methods to the Objective-C runtime. This function is called through the use of a "selector" which is an Objective-C feature, thus you have to expose it with this keyword.