문제

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?

도움이 되었습니까?

해결책

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];
        }
    }

다른 팁

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

}

- (void)btncloseClicked
{
    [YourSearchBar performSelector: @selector(resignFirstResponder) withObject: nil afterDelay: 0.1];
    [table_View reloadData];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top