Maddie Elizabeth Maddie Elizabeth - 5 months ago 15
Objective-C Question

TapGesture to Dismiss Keyboard/Unrecognized Selector

I have a UISearchBar on my page. I want this search bar to disappear when any part of the page is tapped. This is the code I have in my viewDidLoad:

UITapGestureRecognizer *pageTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideSearchBar)];
[self.view addGestureRecognizer: pageTapped];

My hideSearchBar method looks like this:

-(void)hideSearchBar:(UITapGestureRecognizer *)pageTapped{
[self.view endEditing:YES];

I am currently getting an unrecognized selector error, saying that hideSearchBar was an unrecognized selector sent to an instance of filterPageViewController(the class containing hideSearchBar method and the searchBar itself). I'm wondering if its possible I'm missing something in my header file?


You are missing semicolumns in your add action:


should be


or all together:

UITapGestureRecognizer *pageTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideSearchBar:)];

or remove argument from your function:

-(void)hideSearchBar {

if you don't need that page in hideSearchBar method.