Question

SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without automatically triggering the search display controller that is bound to it.

LONG DESCRIPTION OF PROBLEM: I have an iphone application with a search bar and a search display controller. The searchdisplaycontroller is used for autocomplete. For autocomplete i use an sqlite database. The user enters the first few letters of a keyword and the result are shown in the table of the searchdisplaycontroller. An sql select query is executed for every character typed. this part works ok, the letters have been entered and the results are visible.

The problem is the following: If the user selects a row from the table I want to change the text of the searchbar to the text that was selected in the autocomplete results table. I also want to hide the search display controller. This is not working. After the search display controller disappears the textbox in the search bar is empty. I have no idea what's wrong. I didn't think something so simple as changing the text of a textbox can get so complicated.

I have tried to change the text in 2 methods: First in the didSelectRowAtIndexPath method (for the search results table of the search display controller), but that didn't help. The text was there while the search display controller was active (animating away) but after that the textbox was empty.

 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath");

    if (allKeywords.count > 0)
    {
        didSelectKeyword = TRUE;
        self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row];
        NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword);

        shouldReloadResults = FALSE;

        [[self.mainViewController keywordSearchBar] setText: selectedKeyword];

        //shouldReloadResults = TRUE;

        [[mainViewController searchDisplayController] setActive:NO animated: YES];
    }
}

I also tried to change it in the searchDisplayControllerDidEndSearch method but that didn't help either. The textbox was...again.. empty.

edit: actually it wasnt empty the text was there but after the disappearing animation the results of the autocomplete table are still there. So it ends up getting worse.


- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    NSLog(@"searchDisplayControllerDidEndSearch");
    if (didSelectKeyword)
    {
        shouldReloadResults = FALSE;
        NSLog(@"keyword sdc didendsearch: %@", selectedKeyword);

        [[mainViewController keywordSearchBar] setText: selectedKeyword]; //

In this method i call another method which selects the data from sqlite. This part is working.


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSLog(@"shouldReloadResults : %i", shouldReloadResults);
    if (shouldReloadResults)
    {
        NSLog(@"shouldReloadTableForSearchString: %@", searchString);
        [self GetKeywords: searchString : @"GB"];
        NSLog(@"shouldReloadTableForSearchString vege");
        return YES;
    }
    return NO;
}

Please ask me if it's not clear what my problem is. I need your help. Thank you

Was it helpful?

Solution

Didn't you try : mainViewController.searchDisplayController.searchBar.text=selectedKeyword;

You access the search bar field from the search controller and you update its text content.

OTHER TIPS

Now it works i don't know what happened. I think I tried to deactivate the search display controller after changing the text. Anyway thanks for help. I think I also tried to change the original textfield not the one belonging to the search display controller and they are different (different adresses in memory as far as i observed)


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"didSelectRowAtIndexPath");

    if (allKeywords.count > 0)
    {
        didSelectKeyword = TRUE;

        self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row];
        NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword);

        //shouldReloadResults = FALSE;

        [[mainViewController searchController] setActive:NO animated: YES];

        [mainViewController.searchController.searchBar setText: self.selectedKeyword];

    }
}

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