So this is what my view looks like - there's a text field on top acting as a search bar, and then below that there is a table view. I want to implement backgroundTap, so that when I tap anywhere the keyboard goes away if it is up.
I've tried doing this by changing the view to be a UIControl and adding this -
- (IBAction)backgroundTap:(id)sender{
NSLog(@"BACKGROUND TAPPED");
[self.searchBar resignFirstResponder];
}
This doesn't work - the backgroundTap method doesn't run when I click on the tableView (and I've connected things properly).
I also tried overriding the touchesBegan method for the table view, but that didn't work either.
How do I achieve what I'm trying to achieve?
EDIT-
I tried to do this-
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
tapRecognizer.enabled = NO;