1

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

1
  • are you sending the request synchronously?
    – 0xSina
    Feb 20, 2012 at 15:17

3 Answers 3

5

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

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

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.

0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.