Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss the Keyboard using UIsearchBar Delegate methods? i tried every method but no use. [duplicate]

Tags:

ios

iphone

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];
}
like image 977
Logunath Avatar asked Mar 13 '13 13:03

Logunath


2 Answers

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];
}
like image 72
nsgulliver Avatar answered Nov 03 '22 11:11

nsgulliver


Try this:

Set the delegate on the search bar and check whether the IBOutlet is connected or not

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}           
like image 10
Balu Avatar answered Nov 03 '22 12:11

Balu