问题
I have a UISearchDisplayController
(searchDisplayCtr) and a UISearchBar
(searchBar). I am trying to give focus to the search bar and bring up the keyboard when I click the search icon in the alphabet scroll on the right. If I include,
[self.searchDisplayCtr setActive:YES];
[self.searchDisplayCtr.searchBar becomeFirstResponder];
It brings up the keyboard and selects the search bar but the cancel button does not work consistently. I have tried resigning first responder, setting active to NO and both in searchDisplayControllerWillEndSearch
, but it doesn't help. The cancel button will work only if no other letters on the alphabet scroll have been clicked yet.
回答1:
It is apparently necessary to scroll up to the top of the table before setting first responder. The code ended up being
[tableView setContentOffset:CGPointMake(0, 0) animated:NO];
[self.searchBar becomeFirstResponder];
来源:https://stackoverflow.com/questions/21007059/using-becomefirstresponder-causes-cancel-button-to-not-work