How to save text in the search bar upon termination of UISearchDisplayController

I am using UISearchDisplayController in my application. When the user selects an item in the search results, I deactivate the UISearchDisplayController. Deactivating the controller clears the text that the user typed. I want to keep it there. I am trying to assign text back to the UISearchBar by setting it again after the controller has been deactivated. The text appears in the search bar, but this will lead to the active action of the UISearchDisplayController, even though I disabled the delegate! This issue only occurs on iOS 7. Before iOS7, the code below works charmingly.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; NSString *term = [keywordSuggestion objectAtIndex:row]; [search resignFirstResponder]; [self handleSearchForTerm:term]; } -(void)handleSearchForTerm:(NSString *)searchTerm { [searchDisplayController setActive:NO animated:YES]; //searchbar text will be lost searchDisplayController.delegate = nil; search.text = term; searchDisplayController.delegate = self; } 

Is there a way I can set the text of a UISearchBar without associating it with activating the UISearchDisplayController?

+6
source share
1 answer

Here is an example of some working code:

 #import "RBTableViewController.h" @interface RBTableViewController () <UISearchDisplayDelegate> @end @implementation RBTableViewController { UISearchBar *_searchBar; UISearchDisplayController *_searchController; NSMutableArray *_searchResults; NSMutableArray *_model; NSString *_cachedSearchTerm; } - (NSMutableArray *)currentModel { return _searchController.isActive ? _searchResults : _model; } - (void)viewDidLoad { [super viewDidLoad]; _searchResults = [[NSMutableArray alloc] init]; _model = [[NSMutableArray alloc] init]; for (int i = 0; i < 10; i++) { [_model addObject:[NSString stringWithFormat:@"item %d", i]]; } _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self]; _searchController.searchResultsDataSource = self; _searchController.searchResultsDelegate = self; _searchController.delegate = self; [_searchController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; self.tableView.tableHeaderView = _searchBar; } - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { controller.searchBar.text = _cachedSearchTerm; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == _searchController.searchResultsTableView) { _cachedSearchTerm = _searchBar.text; [_searchController setActive:NO animated:YES]; [self filterResults:_cachedSearchTerm]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self currentModel] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; cell.textLabel.text = [self currentModel][indexPath.row]; return cell; } - (void)filterResults:(NSString *)searchTerm { [_searchResults removeAllObjects]; [_searchResults addObjectsFromArray:[_model filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[s] %@", searchTerm]]]; [_searchController.searchResultsTableView reloadData]; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterResults:searchString]; return YES; } - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller { if (_cachedSearchTerm) { controller.searchBar.text = _cachedSearchTerm; } } @end 
+1
source

All Articles