programmatically use UISearchDisplayController
I have an app which asynchronously searches a remote API and displays the UI using iOS' UISearchDisplayController.
For a saved search feature, I've been trying to programmatically use the UISearchDisplayController, in order to both initiate the search, and set up the user interface to be right back where he used to be. (Ie., I'm trying to bring up the search bar and set the search term.)
searchTab开发者_运维技巧leViewController.searchDisplayController.searchBar.text = mySearchTerm;
//[...]
[searchTableViewController.searchDisplayController setActive:YES animated:YES];
[searchTableViewController performSearch];
The code I've tried so far—above—doesn't seem to do the trick. While it correctly brings up the search bar, sets the search term and performs a search, the system doesn't seem to recognize this as a valid search somehow. If I use my fingers in the results view to make the keyboard disappear, the search term resets itself and the results disappear.
Help appreciated. Thanks!
I ran into the same issue. I solved this by creating the ivar NSString * savedSearchString;
and adding the following methods in the delegate for UISearchDisplayController.
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
// saves search string for next search
[savedSearchString release];
savedSearchString = [controller.searchBar.text retain];
return;
}
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
// shows keyboard and gives text box in searchBar the focus
[controller.searchBar becomeFirstResponder];
// shows previous search results
if ((savedSearchString))
controller.searchBar.text = savedSearchString;
return;
}
Adding the line controller.searchBar.text = savedSearchString;
to searchDisplayControllerWillBeginSearch:
would cause the search term to appear in the UISearchBar, however the results table would not be reloaded.
The transition is a little rough if there is a previous search string. If I find better implementation which does not have a rough transition, I'll update my answer.
精彩评论