user3394313
user3394313

Reputation: 43

Methods for multiple search bars in a view

In a view I have 2 search bars that both need to use a separate searchBarTextDidBeginEditing method. However whenever I try to create a seperate method is shows as duplicate. With one method both are calling upon it.

I'm pretty sure i'm heading the methods wrong.

searchBarTextDidBeginEditing:nearSearch searchBarTextDidBeginEditing:whatSearch

nearSearch and whatSearch are two different search bars.

I tried nearSearch: searchBarTextDidBeginEditing but am not sure if that is correct either.

Upvotes: 1

Views: 817

Answers (1)

Daniel
Daniel

Reputation: 22405

The delegate methods return a reference to the search bar object that calls it, a reason for that is precisely the issue you are describing, basically you should have a reference to your search bars, and compare to know which searchbar is calling the method and act accordingly..

say you have defined

UISearchBar *searchBarOne
UISearchBar *searchBarTwo

then in your delegate methods for example

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
      if(searchBar==searchBarOne)
      {
          //handle search bar one 
      }
      else if(searchBar==searchBarTwo)
      {
      //handle search bar two
      }
    }

Another solution is to set different tags for your search bars and compare the tags, as the comment to your question states.

hope that helps

Daniel

Upvotes: 1

Related Questions