I have a text field to search, with an autocomplete list (UITableView
) to show the results. I want to dismiss the keyboard while user clicks outside the textfield, and that works; however it breaks my table interaction(user can clicks one of the results to add a record).
I have tried shouldReceiveTouch
(source1, source2) with UIGestureRecognizer
, however the function doesn't get called. I have
Added
UIGestureRecognizerDelegate
to ViewController.swift as follow:class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate,UIGestureRecognizerDelegate {...}
Added
UITapGestureRecognizer
object into storyboard and linked it to my viewAdded
IBAction
function to dismiss the keyboard and linked it to theUITapGestureRecognizer
object:@IBAction func endEditing(sender: AnyObject) { addButton.hidden = true autocompleteList.hidden = true self.view.endEditing(true) }
Called
shouldReceiveTouch
:func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { println("in shouldReceiveTouch") gestureRecognizer.delegate = self if (touch.view == autocompleteList){ println("touching autocomplete list") return false } else{ println("touching elsewhere") return true } }
After running the app I learned that shouldReceiveTouch
never gets called from the console, so my guess is I didn't set up the delegate correctly; I have been looking here and there for a few hours but still no luck. Other solutions to achieve the same goal are welcome as well. Any help is appreciated! Thanks