我正在使用UItextField执行搜索任务,请点击搜索按钮,它应该开始通过调用网络api进行异步任务,并关闭键盘。但是键盘仍然在那里。这是代码
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
}
我想在点击搜索按钮后关闭键盘,并且异步任务也应该开始。
答案 0 :(得分:0)
您必须辞退第一响应者。
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
attemptPost { (success) in
DispatchQueue.main.async {
spinner.stopAnimation(self)
btnOk.isEnabled = true
btnCancel.isEnabled = true
}
textField.resignFirstResponder()
return true
}
请参阅https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn
上的讨论