Question

I would like to know how can you prevent the UISearchbar from filtering an array of items after each character entry? and instead only do the one search after the search button or enter is pressed after the user has typed their search term.

I only ask as I have implemented a UISearchBar and unfortunately I have some really large data sets I am working with which really slow things down.

This is the UISearchBar I have created

filterSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10.0, 170.0, 280.0, 40.0)];
        filterSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
        UITextField *textField = [filterSearchBar valueForKey:@"_searchField"];
        textField.clearButtonMode = UITextFieldViewModeNever;
        filterSearchBar.delegate = self;
        self.filterSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
        if (([doorSearchString length] != 0) && (doorSearchString != nil)) {
            filterSearchBar.text = doorSearchString;
        }
        filterSearchBar.placeholder = @"Door filter";
        [allBackGroundView addSubview:filterSearchBar];
Was it helpful?

Solution

Use searchBarSearchButtonClicked: delegate method of UISearchBar if you need to search only after pressing search button.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    //Do search logic here
}

Don't forget set the delegate of UISearchBar. If you don't need to filter items after each character entry remove - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText delegate method.

Hope this helps

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