我有一个textView
和一个名为“发送”的按钮(不是来自键盘)。在结束输入评论时,我需要从此textView
发送文字,但第一次点击时我会关闭键盘,第二次点击可以发送准确的评论。
func textViewDidEndEditing(_ textView: UITextView) {
textView.resignFirstResponder()
}
@IBAction func sendComment(_ sender: UIButton) {
comment() // POST to API
}
func comment() {
let params: Parameters = [
"text": commentTextView.text
]
let id = topic?.id
var request = URLRequest(url: url)!)
request.httpMethod = "POST"
request.allHTTPHeaderFields = Settings.sharedInstance.headers
request.httpBody = try! JSONSerialization.data(withJSONObject: params)
Alamofire.request(request)
.responseJSON { response in
switch response.result {
case .failure(let error):
print(error)
if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
print(responseString)
}
case .success(let responseObject):
self.commentTextView.text = ""
print(responseObject)
}
}
}
我需要在键入结束时按一下按钮并发送评论。
答案 0 :(得分:0)
将UIGestureRecognizerDelegate
添加到您的viewcontroller
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
tapGesture.delegate = self
tapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(tapGesture) //instead of view you can use your button here
将此代码添加到您的viewcontroller
中func handleTap(_ sender:UITapGestureRecognizer){
print("double tapped")
}