Unrecognized selector sent to instance for my tap gesture
You don't know how to make the correct selector for this method. (It would be "didtapContainerViewWithGesture:"
, but clearly you don't know that.)
So don't try. Use #selector
syntax and let the compiler form the selector for you!
Just say #selector(didtapContainerView)
. Done.
Swift 2 unrecognized selector in tapgesture
The issue may be that UIKit does not know how to call a selector that throws
, at least in this context.
Remember that UIKit will be calling this function in response to a tap action. It is not accustomed to calling a function that throws
; obviously the throw
must alter the method's call signature as far as the Objective-C runtime is concerned. Beyond that, if your function proFormulaTapGesture
did throw an error, what would UIKit do with the error even if it could catch it?
I'm not sure where you added do
/try
/catch
. But unless it was inside your action function proFormulaTapGesture
, I do not see how it could be relevant. I believe that you will need to implement the try
/catch
error handling inside proFormulaTapGesture
, so that it effectively swallows the error, so that proFormulaTapGesture
does not throw.
I just created a quick test project myself and found that a throw
ing target action gives the same "Unrecognized selector" error you got:
override func viewDidLoad() {
super.viewDidLoad()
self.tapper = NSClickGestureRecognizer(target: self, action: "didClick")
self.view.addGestureRecognizer(self.tapper!)
}
func didClick() throws {
print("did click")
}
2015-07-27 00:16:43.260 Test1[71047:54227175] -[Test1.ViewController didClick]: unrecognized selector sent to instance 0x6000000c5010
...
I believe you will need to rework your code to handle the error internally in your action function so that it does not throw - something like this:
func proFormulaTapGesture() {
print("proFormulaTapGesture")
selectView(proFormulaView)
selectedMenuItem = 0
Formula = 1
menuTabBarController.selectedIndex = 0
navigationController?.navigationBar.topItem!.title = "BASIC FORMULA"
do {
try (menuTabBarController.viewControllers as! [SuggestionsViewController])[0].loadSuggestions()
} catch {
// Handle the error here somehow
}
}
If you are unable to handle the error within proFormulaTapGesture
, you'll have to be more creative with passing it out of the function (think NSNotification, etc...).
To be clear, I created a new Single View iOS application in Xcode 7 beta 4, and used this exact code as my ViewController. No other changes. The app compiles and works just fine in the iOS simulator.
@robertvojta also points out that if didTap
below is marked private
, @objc
is also required to avoid a dynamic dispatch crash.
import UIKit
class ViewController: UIViewController {
var tapper: UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
self.tapper = UITapGestureRecognizer(target: self, action: "didTap")
self.view.addGestureRecognizer(self.tapper!)
}
func didTap() {
print("did Tap")
}
}
(Note I also tested on an OS X project, which is the code I used earlier in the answer).
Swift 2 TapGesture Error: unrecognized selector sent to instance
One problem is that your action:
selector, "score:"
, doesn't correspond to what Objective-C sees when you declare your method as func score(sender:UITapGestureRecognizer!) throws
. It sees "score:error:"
. The simplest solution is to delete throws
, since "score:error:"
cannot be an action method signature for a tap gesture recognizer.
Moreover, as @dan has pointed out, score
is not in self.view
but in self
. So you also need to change your target:
.
Swift: Gesture Recognizer unrecognized selector sent to instance
Please, give Neo credit. You need to change your syntax to this:
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleTap)
Side note: For Swift you do not need semi-colons the end your code lines.
Unrecognized selector error trying to use a tap gesture recognizer
The error indicates that you're incorrectly calling your select(_:)
function on a boolean
:
'-[__NSCFBoolean select:]: unrecognized selector sent to instance 0x10ad5a690'
After examining your code to see where and how you've called select(_:)
, it becomes clear that the problem is that you're setting your UITapGestureRecognizer
's target to a boolean, i.e. true
:
let tap = UITapGestureRecognizer(target: true, action: #selector(SignupViewController.select(_:)))
when it should be set to your function's view controller. For example, in this case, you probably want to set your target to self
:
let tap = UITapGestureRecognizer(target: self, action: #selector(SignupViewController.select(_:)))
As for the select(_:)
method you're calling, it seems to me that you made a typo and that you meant to call the selectPhoto(tap:)
method you've created instead; in which case, your tap gesture declaration and initialization should instead be:
let tap = UITapGestureRecognizer(target: self,
action: #selector(SignupViewController.selectPhoto(tap:)))
Unrecognized selector error when defining tap gesture in custom view [Swift]
It turns out that it was related to part of my code where I said unrelated code.
I was changing calculating relative position of my custom view. I was changing frame of contentView
, which was the wrong part. Instead I manipulated self
. Now everything work as I want.
Working version my function:
func showTip(viewToAlign: UIView, viewToAdd: UIView){
self.userInteractionEnabled = true
let relativeFrame = viewToAlign.convertRect(viewToAlign.bounds, toView: nil)
let relativeCenter = viewToAlign.convertPoint(viewToAlign.bounds.origin, toView: nil)
self.frame = CGRectMake(relativeFrame.minX - (self.frame.size.width + 5), relativeCenter.y - self.frame.size.height/2 , self.frame.size.width, self.frame.size.height)
self.layer.masksToBounds = false
self.layer.shadowOffset = CGSizeMake(0, 0)
self.layer.shadowRadius = 5
self.layer.shadowOpacity = 0.5
viewToAdd.addSubview(self)
tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(closeView))
viewToAdd.addGestureRecognizer(tapGesture!)
}
Swift: Unrecognized selector sent to instance error when trying to use tap gesture
Don't use Selector()
. Use the #selector()
form. The compiler is able to check for a matching method with that form.
And for a gesture recognizer, the selector should have 1 parameter: The gesture recognizer itself:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
And your function would look like this
@IBAction func tap(_ gesutureRecognizer: UITapGestureRecognizer) {
}
For a function of a UIViewController
you shouldn't need the @objc
qualifier on the function, since a UIViewController
is an Objective-C object.
Unrecognized selector with UITapGestureRecognizer
The target of Gesture is (self) not self.view because you are calling all the viewcontroller not just the view inside him,
let viewTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
view.addGestureRecognizer(viewTapRecognizer)
and if you want to reduce hideKeyboard function just use
view.endEditing(true)
Related Topics
Persist Accessibility Permissions Between Builds in Xcode 13
Swift Generic Protocol Function Parameters
Swift: Casting a Floatingpoint Conforming Value to Double
Calling a Child Inside 2 Levels of Nodes
What Does This Mean? Variable Declared Followed by a Block Without Assignment
Swift 3:Delegate Within Tapgesturerecognizer in Generics Doesn't Get Called
Swiftui - Navigation View Opening with Back Button and Half Grey Screen/Weird Behavior
Aws - Unauthenticated Access Is Not Supported for This Identity Pool in Swift
Swift Cannot Output When Using Nstimer
Swift: Ambiguous Reference to Member 'Map'
How to Decrease a Value Using Fieldvalue in Firestore (Swift)
Cannot Preview This File, App May Have Crashed -- Occurs When Inputting Specific Line of Code
Self. in Trailing Swift Closures, Meaning and Purpose
Swift: Implementing Protocol Initializer in a Class
Swift Deleting Table View Cell When Timer Expires
Why Does the Compiler Not See the Default Code in a Protocol