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.
Related
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.
I have read many tutorials and even the official Apple documentation and must not understand what is wrong with this code.
var dueDatePicker = UIDatePicker()
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.inputView = dueDatePicker
dueDatePicker.addTarget(self, action: #selector(datePickerValueChanged(_:)), for: UIControlEvents.valueChanged)
}
func datePickerValueChanged(_ sender: UIDatePicker){
//Do Stuff
}
At runtime, I click on the textField and the UIDatePicker appears. The function that the selector points to is executed. As soon as I click a UI object outside of the UIDatePicker, the app crashes with this error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[YourApp.PromiseViewController
dueDateChanged:]: unrecognized selector sent to instance 0x100b12ae0'
What I don't understand is that the "selector" or pointer to the desired function is recognized initially. However, when I trigger another event from another UI Object this exception is thrown.
Why is this happening?
Shouldn't this exception be triggered when datePickerValueChanged() is called initially?
Just add #objc in front of your function
#objc func datePickerValueChanged(_ sender: UIDatePicker){
//Do Stuff
}
The error is telling you that an action with the selector dueDateChanged(_:) has been added as a target action.
More than one target action can be added to a control. Somewhere, maybe in your storyboard or xib, you have another action added to dueDatePicker.
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.
I've spent 2 days trying to get a single tap to work on a single view (clicking anywhere in the screen). I've tried every variation of fixing this problem I could find. Inside the DVC Class, with #IBActions on everything, renaming the view as an #IBAction etc. I can't get any other error except : "[UIView score:]: unrecognized selector sent to instance"
class DataViewController: UIViewController, UIGestureRecognizerDelegate {
var tappy = UITapGestureRecognizer()
override func viewDidLoad() {
self.tappy = UITapGestureRecognizer(target:self.view, action:"score:")
self.tapView!.addGestureRecognizer(self.tappy)
}
}
func score(sender:UITapGestureRecognizer!) throws {
print("tapped")
}
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:.
I add a long press in my collectionviewcell with
lp = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))
lp.delegate = self
cell.tag = indexPath.row
cell.addGestureRecognizer(lp)
but if I push to other viewController and popback with swipe, the project will crash!
So I look for website to solve this problem, and I know the reason is I don't get a UIGestureRecognizer for delegate
func gestureRecognizerShouldBegin(g: UIGestureRecognizer) -> Bool {
return true
}
And I got this exception
gestureRecognizerShouldBegin:]: unrecognized selector sent to instance 0x7fa9e0d8b7c0
2015-03-15 18:56:08.733 PicMemo[62182:5214628]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PicMemo.iViewController gestureRecognizerShouldBegin:]: unrecognized selector sent to instance 0x7fa9e0d8b7c0'
I think I got a solution in uilongpressgesturerecognizer crashes even if not implemented
But I don't know how to do with Swift.
Thanks for #HotLicks
There is a solution:
We should add UIGestureRecognizerDelegate
Then add
self.navigationController?.interactivePopGestureRecognizer.delegate = self
finally
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
//self.navigationController?.popViewControllerAnimated(true) is also ok
self.navigationController?.popToRootViewControllerAnimated(true)
return true;
}