0
votes

I'm trying to preload UISearchBar with a searchString.

In the current set up. I'm using UISearchDisplayController in the UITableViewController.

The First approach I use is set the search string in the following method:

- (void) presetSearchDisplayControllerWithString:(NSString*)searchString{
    [self.searchDisplayController setActive:YES animated:NO];
    [self.tableView setContentOffset:CGPointMake(0, 0)];
    self.searchBar.text = searchString;
    self.searchFetchedResultsController = [self   createFetchedResultsControllerWithSearchTerm:searchString];
}

It works. UISearchbar shows the search string, original tableView and TableViewIndex are hidden. But if I press cancel button on the searchBar it crashes:

* Assertion failure in -[UISearchDisplayController setActive:animated:], /SourceCache/UIKit_Sim/UIKit-2372/UISearchDisplayController.m:682 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'search contents navigation controller must not change between -setActive:YES and -setActive:NO'*

The Second varian is not working either

- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
    [self.searchDisplayController.searchBar setText:@"rune"];
}

Well, it works if you tap in the searchbar. But until you've done it, it shows just a regular tableView.

1
Did the answer solve your problem?Zaphod
No. I ended up not using searchDisplayController. Not sure, but I think I tried your combination as well, and it didn't workEugene
Ok, thanks for sharing.Zaphod

1 Answers

0
votes

Have you tried something like this (only based on the exception raised):

[self.searchDisplayController setActive:NO animated:NO];
self.searchBar.text = searchString;
[self.searchDisplayController setActive:YES animated:NO];
[self.tableView setContentOffset:CGPointMake(0, 0)];
self.searchFetchedResultsController = [self createFetchedResultsControllerWithSearchTerm:searchString];