当我想要时,UISearchBar 不回答 resignFirstResponder
我有一个带有附加 UISearchBar 的 UITableView。当用户点击键盘中的搜索键时,我想向服务器发送请求以获取一些信息。一切工作正常,但 resignFirstResponder 在发送请求之前不会关闭键盘。这就是我所拥有的
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.mySearchBar resignFirstResponder];
[self.brain searchInfo: mySearchBar.text];
[self.tableView reloadData];
}
,我也尝试过使用
[self.view endEditing:YES];
但没有成功......
I have a UITableView with an attached UISearchBar. When the user taps the search key in the keyboard I want to send a request to a server to get some information. Everything works fine, but the resignFirstResponder doesn't close the keyboard until the request has been sent. This is what I have
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.mySearchBar resignFirstResponder];
[self.brain searchInfo: mySearchBar.text];
[self.tableView reloadData];
}
I have also tried to use
[self.view endEditing:YES];
but without succes...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来像是一个简单的线程问题。试试这个...
Looks like a simple threading issue. Try this...
只有在运行循环结束后才会执行对 UI 的更新。这意味着 resignFirstResponder 仅在
searchBarSearchButtonClicked:
方法(及其调用方法)完成后才会生效。由于您的searchInfo:
方法必须等待直到获得响应,因此 resign 方法在此之前将不起作用。重写您的应用程序以使用发送异步请求的 NSURLConnection。 Apple 有一篇关于如何做到这一点的文章< /a>.这是更多的工作,但你应该走这条路来提高你的应用程序的质量。
Updates to the UI are only performed once the runloop ends. This means that the resignFirstResponder will only have an effect once your
searchBarSearchButtonClicked:
method (and it's calling methods) have finished. Since yoursearchInfo:
method has to wait until it gets the response, the resign method will have no effect prior to that.Rewrite your App to use an NSURLConnection which sends an asynchronous request. Apple has an article about how you do that. It is more work, but you should go this route to improve the quality of your App.
看起来 searchInfo: 方法需要很长时间。您可以将其更改为异步工作,并在请求完成后在其他位置刷新 tableView。
Seems like searchInfo: method takes a long time. You can change it to work asynchronously and refresh tableView in other place, once your request done.