Programmatically use the UISearchDisplayController

I have an application that asynchronously searches for a remote API and displays the user interface using iOS 'UISearchDisplayController.

For the saved search function, I try to programmatically use the UISearchDisplayController to both initiate the search and customize the user interface when it was where it was before. (That is, I'm trying to open the search bar and ask a search query.)

searchTableViewController.searchDisplayController.searchBar.text = mySearchTerm;
//[...]
[searchTableViewController.searchDisplayController setActive:YES animated:YES];
[searchTableViewController performSearch];

The code I tried so far is not like a trick. As long as it correctly displays the search string, sets the search query and performs the search, the system does not seem to somehow recognize this as a valid search. If I use my fingers in the presentation of the results so that the keyboard disappears, the search term is reset and the results disappear.

ex1ex2

Help evaluate. Thank!

+3
source share
1 answer

I ran into the same problem. I solved this by creating ivar NSString * savedSearchString;and adding the following methods to the delegate for the 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 a line controller.searchBar.text = savedSearchString;to searchDisplayControllerWillBeginSearch:will cause the search term to appear in the UISearchBar, however the results table will not be reloaded.

, . , , .

0

Source: https://habr.com/ru/post/1778449/


All Articles