In some View Controller I have the following commands:
//Looks for single or multiple taps.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginVC.dismissKeyboard))
//add tap gesture
view.addGestureRecognizer(tap)
but after changing language from swift3 to swift4 I have started receiving the following error
[iosapp_v147.ChangePasswordViewController dismissKeyboard]: unrecognized selector sent to instance 0x155e04090
Any help rendered will be greatly appreciated
The error message is indicating that the target object (a ChangePasswordViewController) receiving the dismissKeyboard call has no function declared as #objc dismissKeyboard()
Make sure that ChangePasswordViewController has a dismissKeyboard function declared with the #objc attribute. That's required for all #selector references.
You should also be able to declare the selector without the "LoginVC." prefix, since the your target is self.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
Finally, make sure the function signature has no labels (dismissKeyboard(this:that:)).
If it's none of those things, post the function declaration.
Related
This question already has answers here:
Swift: Gesture Recognizer unrecognized selector sent to instance
(2 answers)
Closed 5 years ago.
I'm trying to rewrite this line in Swift 3
Swift 2:
let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressDetected")
Swift 3:
let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("longPressDetected")))
but it seems to be throwing this error
unrecognized selector sent to instance
The function that is's trying to call is this:
func longPressDetected(_ sender: Any){}
The corresponding selector for
func longPressDetected(_ sender: Any){}
in Swift 3 is
#selector(longPressDetected(_:))
or even simply
#selector(longPressDetected)
Side-note: Since the sender is a distinct type you should specify this:
func longPressDetected(_ sender: UILongPressGestureRecognizer){}
and don't annotate types the compiler can infer.
I have updated my code to Swift 3 and now getting the error above. I think there is something wrong with the way selector is called. Anyone please help me, What is wrong.
Just change your selector syntax with swift 3 selector syntax like this.
#selector(self.hideKeyboard)
The syntax for Selector has been changed in Swift 3. You don't need to add round brackets after selector name if there is no parameter in the selector.
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
self.view.addGestureRecognizer(tapGesture)
For anyone looking for a solution can try this.
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
view.addGestureRecognizer(tap)
// function which is triggered when handleTap is called
func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
I know it's loo late to answer, but it can help others.
and thanks in advance for taking the time to help.
Inside my CellForRowAtIndexPath, I have the following line:
cell.timeView.addTarget(self, action: "ButtonDidPress:", forControlEvents: UIControlEvents.TouchUpInside)
and my selector function is:
func ButtonDidPress (sender: DesignableView!){
let view:DesignableView = sender
cell.timeView.shadowColor = UIColor.yellowColor()
table.reloadData()
}
and the error i get is:
unrecognized selector sent to instance
I'm thinking that perhaps one can't send a View as a selector (am I using the correct terminology?), but how else can I target that particular view in that cell?
UPDATE:
I also tried using gestureRecognizer instead:
var tap = UIGestureRecognizer(target: self, action: Selector( "viewDidTap:"))
cell.timeView.addGestureRecognizer(tap)
and
func viewDidTap (sender: DesignableView!){
but I got the same error here.
Thanks!
There's a couple of strange things happening in your code. It seems you want to change the shadowColor property of timeView when a user touch it, right?
Two possible solutions are:
(This one is IMO the better one) Change DesignableView to inherit from UIButton. Then you can set:
timeView.addTarget(self, action: "ButtonDidPress:", forControlEvents: .TouchUpInside). Make sure you set it just once for each cell. Otherwise you will get multiple calls on one tap.
Use UITapGestureRecognizer, but you should put it in your UITableViewCell subclass, not to the view controller. Also, the sender in viewDidTap is not the view itself, but the recognizer. So the method will go like this:
func viewDidTap(sender: UITapGestureRecognizer) {
let location = sender.locationInView(sender.view)
if timeView.hitTest(location, withEvent: nil) == timeView {
timeView.shadowColor = UIColor.yellowColor()
// table.reloadData() - you don't need to reload the table
}
}
In a SpriteKit game, I am trying to create a UIPanGestureRecognizer without using Interface Builder, so in the didMoveToView method of my SKScene, I wrote this :
let panGestureRecogniser = UIPanGestureRecognizer(target: view, action: "didPan:")
view.addGestureRecognizer(panGestureRecogniser)
Still in my SKScene class, I wrote this function :
func didPan(sender:UIGestureRecognizer) {
println("Panned")
}
My issue is that when I run my app, and when I pan on the screen, this error in thrown :
2015-05-12 19:28:01.955 Game[7342:2394353] -[SKView didPan:]: unrecognized selector sent to instance 0x154520690
I don't understand what's wrong, I have tried to move the function in both my view controller and app delegate, but it doesn't seem to make any difference...
Change
let panGestureRecogniser = UIPanGestureRecognizer(target: view, action: "didPan:")
To
let panGestureRecogniser = UIPanGestureRecognizer(target: self, action: "didPan:")
I have:
var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "dismissKeyboard")
swipe.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipe)
and
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
func dismissKeyboard() {
self.messageTextView.resignFirstResponder()
}
}
When I swipe down on the device I get the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppName.ViewController dismissKeyboard]: unrecognized selector sent to instance 0x125d06500'
I have a lot of other code in my viewDidLoad plus my messageTextView is inside of a UIView which is in a UITableView. What could be the problem?
Your dismissKeyboard method is nested in your didReceiveMemoryWarning method. This is wrong. You have to declare your dismissKeyboard method as a method of your view controller. Then it should work.
By the way. You say that your messageTextView is embedded in a table view? The UITableView class already offers an opportunity to dismiss the keyboard on swipe. You simply have to set its dismissMode like this:
tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
Checkout the Documentation for UIScrollViewKeyboardDismissMode.