Pergunta

I have a iPhone app that includes a UITableView with a UISearchBar at the top of it. When a user types into the UISearchBar the UITableView's contents are filtered appropriately. When the user deletes all text in the UISearchBar the search bar ceases to be the first responder and the tableview becomes unfiltered once again. Here is the relevant delegate code:

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if(searchBar.text.length == 0)
    {
        [self.searchBar resignFirstResponder];
        [self.myModel stopFiltering];
        [self.tableView reloadData];
    }
}

The problem with this is, when a user speaks into their phone and attempts to do voice-to-text conversion (via Siri) in the search bar, the textdidChange method returns an empty string as search text. Thus I have no way of knowing if the user is speaking into their phone or if they deleted all of the text in the field. Is this behavior expected?

Foi útil?

Solução

Try this and check if you can step into the IF statement :

 - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        if([searchText length]== 0)
        {
            [self.searchBar resignFirstResponder];
            [self.myModel stopFiltering];
            [self.tableView reloadData];
        }
    }

Outras dicas

if([searchText length]> 0)
{
    [self searchTableView];
}
else
{
    [self btncloseClicked];

}

- (void)btncloseClicked
{
    [YourSearchBar performSelector: @selector(resignFirstResponder) withObject: nil afterDelay: 0.1];
    [table_View reloadData];
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top