1

I’m doing a searching task using UItextField and I tap search button it should begin async task fo calling web api and also dismiss the keyboard. But keyboard remains there. Here is the code

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

   attemptPost { (success) in
       DispatchQueue.main.async {
        spinner.stopAnimation(self)
        btnOk.isEnabled = true
        btnCancel.isEnabled = true
    }

     // after this keyboard should dismiss but does not do so
      return true
}

func attemptPost(_ completion:@escaping (Bool)->()) {
    // some code
}

I want keyboard to dismiss once we tap search button and async task should also start.

Manisha
  • 372
  • 3
  • 13

1 Answers1

0

You must resign the first responder.

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

   attemptPost { (success) in
       DispatchQueue.main.async {
        spinner.stopAnimation(self)
        btnOk.isEnabled = true
        btnCancel.isEnabled = true
    }

   textField.resignFirstResponder()
      return true
}

See the discussion at https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn

pkc456
  • 8,350
  • 38
  • 53
  • 109