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;
}
Related
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.
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.
Inside an extension for UIButton, I create a button and add its target.
extension UIButton {
convenience init(target: AnyObject) {
self.init(type: .system)
self.addTarget(target, action: Selector("setLang:"), for: .touchUpInside)
}
}
In ViewController, when using the custom init function to create the button, as target, I pass self.
This worked fine before upgrading my code to Swift 3. Now, however, I receive an error when tapping the button saying:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyApp.ViewController setLang:]: unrecognized selector sent to instance xxx
Inside ViewController there is the following function declared that is supposed to be called:
func setLang(button: UIButton) {
//...
}
Any ideas?
Thanks in advance!
i found that solution from apple developer Forums. This may help you.
selectionButton.addTarget(self, action: #selector(SingleQuestionViewController.selected(_:)), for: .touchUpInside)
// try like this , i hope it will work for you.
self.addTarget(target, action: NSSelectorFromString("setLang:"), for: .touchUpInside)
Found the problem. Instead of declaring the function like I did before, I need to do it the following way:
func setLang(_ button: UIButton) {
//...
}
Hope this helps other peeps.
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 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.