Question

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...

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top