SearchBarTextDidBeginEditing delegate method called twice

I am debugging my code where the delegate method UISearchBar searchBarTextDidBeginEditing: is called exactly twice every time I click on the search bar (which is in the navigation bar).

It is odd that only this delegate method is called twice. Others are called only once during the whole process, which is the correct behavior.

 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { // called only once return YES; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { // called twice every time [searchBar setShowsCancelButton:YES animated:YES]; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { // called only once [searchBar setShowsCancelButton:NO animated:YES]; return YES; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { // called only once } 

Any idea what could be wrong?

UISeachrBar is configured in Storyboard with correctly connected outputs, although it is not added to any view, and in a specific view controller, viewDidLoad is the following line, which adds a search bar to the navigation bar:

 self.searchDisplayController.displaysSearchBarInNavigationBar = YES; 

I use Xcode 5.0.1 and run the code in iOS 7.0.3 Simulator.

+6
source share
4 answers

I ran into the same problem and went a little deeper.

In my case, I had a subclass of UISearchDisplayController , which functioned as UISearchDisplayDelegate for myself, and UISearchBarDelegate for its UISearchBar .

As it turns out, the problem is that the UISearchDisplayController implements the following methods that collide with the UISearchBarDelegate` protocol:

 - (void)searchBar:(id)arg1 textDidChange:(id)arg2; - (void)searchBarCancelButtonClicked:(id)arg1; - (void)searchBarResultsListButtonClicked:(id)arg1; - (void)searchBarSearchButtonClicked:(id)arg1; - (void)searchBarTextDidBeginEditing:(id)arg1; 

This means that if you make UISearchDisplayController delegate of your UISearchBar , these methods will be called twice.

+2
source

I found that if you canceled the searchBar delegate and only saved the searchDisplayController delegate, this method is no longer called. So the only solution I could come up with is to put this at the top of your search for BarTextDidBeginEditing and searchBarTextDidEndEditing.

 static NSDate *lastInvocation; if ([[NSDate date] timeIntervalSinceDate:lastInvocation] < 0.1f) { lastInvocation = [NSDate date]; return; } else { lastInvocation = [NSDate date]; } 
0
source

I found this solution: [searchBar setShowsCancelButton: NO animated: YES]; [searchBar resignFirstResponder]; But it’s funny that I deleted it after two rounds of testing, the code is just called once, not twice

0
source

I had a problem with searchBarSearchButtonClicked: the method is called twice. The problem was resolved by calling [searchBar resignFirstResponder];

  #pragma mark - UISearchViewDelegate methods - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ // Do things [searchBar resignFirstResponder]; } 
0
source

Source: https://habr.com/ru/post/957771/


All Articles