当我想要时,UISearchBar 不回答 resignFirstResponder

发布于 2025-01-07 03:35:39 字数 434 浏览 0 评论 0原文

我有一个带有附加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看起来像是一个简单的线程问题。试试这个...

[searchBar performSelectorOnMainThread:@selector(resignFirstResponder) withObject:nil waitUntilDone:NO];

Looks like a simple threading issue. Try this...

[searchBar performSelectorOnMainThread:@selector(resignFirstResponder) withObject:nil waitUntilDone:NO];
影子是时光的心 2025-01-14 03:35:39

只有在运行循环结束后才会执行对 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 your searchInfo: 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.

拥抱影子 2025-01-14 03:35:39

看起来 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文