Hi i am using a UIsearchBar in my app, i tried all methods in UISearchBar Delegate to dismiss the keyboard but no use. could anyone help me out.
some codes i tried
1.
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
2.
-(BOOL) searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"])
{
[searchBar resignFirstResponder];
return NO;
}
return YES;
}
3.
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
[searchBar resignFirstResponder];
}
First of all you should implement UISearchBarDelegate
in your class & check if you have connected the to your UISearchBar
or not?
If you are adding UISearchBar
through IB then you should select delegate property of the UISearchBar to the file's owner. You should make sure that you are connecting IBOutlet
for the UISearchBar
in the code properly .
If you are adding UISearchBar
via code then you should set the delegate yourself in the code like this searchBar.delegate=self;
Next step is to make sure that delegate methods are being called, which is obvious if you have connected delegate properly, best thing to check stuff is putting break pointer in your code or print through NSLog
.
Finally use this method to hide the keyboard if you want to hide when user clicks to search
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
OR you could try when editing finished
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
Try this:
Set the delegate on the search bar and check whether the IBOutlet is connected or not
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With