UISearchBar and resignFirstResponder

落花浮王杯 提交于 2019-12-23 07:00:07

问题


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.


回答1:


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

[self.view endEditing:YES];

all views will resign first responder.




回答2:


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];
}


来源:https://stackoverflow.com/questions/3424172/uisearchbar-and-resignfirstresponder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!