Updating the UISearchBar programmatically
Today I had to update WelliBUS’s search bar programmatically because I needed to use the street picked up via GPS as my street name.
Until this point implementing the search bar seemed easy:
- adopt a couple of protocols: UISearchDisplayDelegate and UISearchBarDelegate
- implement a few callback methods (from the delegates listed above)
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString; - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption; - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller; - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller;
- and implement my own filtering function
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope;
The problem I had on my hands was how to programmatically update the text in the UISearchBar and force the UISearchDisplayDelegate to call the appropriate callback methods.
Turns out it was simpler than expected (despite not being able to find a solution by googling). All that was needed was:
[self.searchDisplayController.searchBar becomeFirstResponder]; self.searchDisplayController.searchBar.text = returnString;
Cheers…
By Nick | 16. Oct 2009 | Objective C, Tutorial, iPhone | No Comments »
