S. Braun S. Braun - 2 months ago 12
iOS Question

Resetting UISearchbar programmatically

Is there a possibility to reset a UISearchbar with searchDisplayController programmatically which is active and already contains text?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == self.searchDisplayController.searchResultsTableView) {
searchDisplayController.searchResultsTableView.hidden = YES;
searchBar.showsCancelButton = NO;
[searchBar resignFirstResponder];
}
}


This solution is working, but there is still text in the searchBar. If i add those lines of code:

searchBar.text = @"";


There is always a black transparent view left.

Any solutions?

Answer

Here ya go. This is a delegate method called when the user hits cancel. If you want to wire it up differently just declare your search bar as an outlet and reference it. Anyway:

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.text = @"";
    [searchBar resignFirstResponder];   
}
Comments