UISwipeGestureRecognizer for swipe down keyboard error - ios

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.

Related

dismissKeyboard - unrecognized selector

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.

How to properly use selectors in swift 4

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.

Having trouble targeting a DesignableView in a UITableViewCell upon tapping it in order to change

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
}
}

Pan gesture recognizer selector won't work

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:")

Swift how to solve unrecognized selector sent to instance 0x7fa9e0d8b7c0

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;
}

Resources