1

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 view

  • Added IBAction function to dismiss the keyboard and linked it to the UITapGestureRecognizer 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

Community
  • 1
  • 1
amythegreat
  • 11
  • 1
  • 4
  • 1
    have you set the delegate implementer to your ViewController class in story board? – Sanjay Mohnani Apr 16 '15 at 08:47
  • also please check if the gesture recognizer exist at the top of all other subviews and return false for views which should be excluded from gesture recognizer in shouldReceiveTouch – Sanjay Mohnani Apr 16 '15 at 08:50
  • @SanjayMohnani you saved my day. I didn't set the delegate in the connection inspector. Now it calls the function! Thanks so much – amythegreat Apr 17 '15 at 00:39

0 Answers0