Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer with UISearchBar

I have a UISearchBar on my main ViewController. Below this search bar is a map so I figure that I need a second ViewController specifically for searching.

My current approach is to add a uitapgesturerecognizer to the search bar on my main ViewController so that when the user clicks on the search bar, they are seamlessly redirected to the second view.

My code:

UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                                     action:@selector(segueToSearch:)];

[searchBar addGestureRecognizer:singleFingerTap];

and

- (IBAction)segueToSearch:(id)sender {
    NSLog(@"about to segue");
    }

This works when I do singleFingerTap.numberOfTapsRequired = 2 but obviously I want it to work with one tap.

Is my approach valid and if so what is wrong or is there another way to do this?

like image 988
scottmrogowski Avatar asked Sep 01 '25 16:09

scottmrogowski


1 Answers

UISearchBar has a UISearchbarDelegate protocol which you can use to get notified when the users attempts to edit the inputfield.

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
   // here you open the viewController
   return NO;
}
like image 69
Karl Avatar answered Sep 04 '25 06:09

Karl