UISearchBar 和 resignFirstResponder

发布于 2024-09-13 08:31:42 字数 426 浏览 3 评论 0原文

我有一个非常基本的 UITableView,带有附加的 UISearchBar,下面是 UITableView 为空时发生的流程

,用户点击 UISearchBar,然后调出键盘。 一旦用户点击“搜索”按钮,

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [searchBar resignFirstResponder]; //move the keyboard out of the way
    //Code....  
}

效果就很好,并将键盘移开,并填充 UITableView。 问题在于任何后续的搜索尝试。

发生与之前相同的步骤,但是键盘永远不会消失。我有一种感觉,有其他东西正在成为响应者,我只需要一点清晰度来理解实际发生的事情。

I have a very basic UITableView with an attached UISearchBar, and here's the flow of what happens

UITableView is empty, user taps UISearchBar, and brings up keyboard.
Once the user taps the Search button

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [searchBar resignFirstResponder]; //move the keyboard out of the way
    //Code....  
}

Works just fine, and moves the keyboard out of the way, and populates the UITableView.
The problem is any subsequent search attempts.

The same steps as before occur, however the keyboard is never dismissed. I have a feeling something else is becoming the responder, I just need a little clarity to understand what is actually occurring.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心如狂蝶 2024-09-20 08:31:42

如果没有看到你的代码,很难猜测。但是,如果您包括:

[self.view endEditing:YES];

所有视图都将辞去第一响应者。

Without seeing your code it is difficult to guess. However, if you include:

[self.view endEditing:YES];

all views will resign first responder.

天气好吗我好吗 2024-09-20 08:31:42

并不完美,但确实适合我的情况。如果没有 dispatch_after 将无法工作

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (!searchText.length) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [searchBar resignFirstResponder];
        });   
    }
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [self performSearchWithString:searchBar.text];
    [searchBar resignFirstResponder];
}

Not perfect but did work for my case. Will not work without dispatch_after

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (!searchText.length) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [searchBar resignFirstResponder];
        });   
    }
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [self performSearchWithString:searchBar.text];
    [searchBar resignFirstResponder];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文