How i dismiss keyboard on tapping search button on keyboard regarding UISearchBar?

回眸只為那壹抹淺笑 提交于 2019-12-23 06:23:11

问题


I m developing an application in which i adding one control UISearchBar. When i started editing text in UIsearchBar then keypad is animated on the screen. After i completed my complete editing or canceling all text then i will stuck on point of dismissing keypad. How i dismiss keyboard on tapping search key form UIKeypad?

Also same question for UITextField and UITextView?

Thanks


回答1:


Make sure the view controller which has the SearchBar implements the SearchBarDelegate and you set the searchBar.delegate to self:

@interface AddressSearchViewController : UIViewController <UISearchBarDelegate>

then implement the following method:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

This will make the keyboard disappear when you tap the search button on the keyboard or the search bar




回答2:


For the search bar:

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
   [searchBar resignFirstResponder];
}

And same thing for the text field:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}



回答3:


Use this code in ViewDidLoad

-(void) ViewDidLoad
 {
    [super ........];
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.cancelsTouchesInView = NO;    
 }

 - (void) hideKeyboard 
 {
    [texfieldname1 resignFirstResponder];
    [texfieldname2 resignFirstResponder];
 }


来源:https://stackoverflow.com/questions/8558339/how-i-dismiss-keyboard-on-tapping-search-button-on-keyboard-regarding-uisearchba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!