无法在UISearchBar中单击搜索按钮时隐藏键盘

时间:2013-05-02 12:34:03

标签: ios objective-c uisearchbar uikeyboard

我使用UISearchBar搜索列表,但是当我点击搜索按钮时 在键盘上,键盘不会隐藏 请帮我解决这个问题。
这是我的代码

-(void)search:(UISearchBar*)searchbar Text:(NSString*)text
{
    [copyListOfItems removeAllObjects];
    if([text length] > 0) {
    //  [ovController.view removeFromSuperview];
    searching = YES;
    NSString *searchText = searchBar.text;
           for(imergencyData *objtrust in aryTrustee)
          {
          NSRange titleResultsRange = [objtrust.strName rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:objtrust];
    }
    NSLog(@"number of object found %d",copyListOfItems.count);
}
else
    {
    //  [tblTrustee insertSubview:ovController.view   aboveSubview:self.parentViewController.view];
    [searchbar resignFirstResponder];
    searching = NO;
    //tblTrustee.scrollEnabled = NO;
}
[tblTrustee reloadData];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
 {
       [aSearchBar resignFirstResponder];
       [self.view endEditing:YES];
 }

 //Method call when type text in search box
-(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
        [self search:theSearchBar Text:searchText];

}
//method call when on search button
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBa{
        [searchBa endEditing:YES];
        [searchBa resignFirstResponder];
        [self search:searchBa Text:searchBar.text];

}

8 个答案:

答案 0 :(得分:4)

把这行代码试一试,

- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar {
  [aSearchBar resignFirstResponder];
  [self.view endEditing:YES];
}

(或) EDIT

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
      [searchBar resignFirstResponder];
      [self.view endEditing:YES];
  }

答案 1 :(得分:0)

先设置searchbar.delegate = self,然后设置- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar来电[searchbar resignFirstRespnder]

祝你好运

答案 2 :(得分:0)

您可以使用单击搜索栏搜索按钮时调用的下方委托。

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{    

    [theSearchBar resignFirstResponder];

 }

或者将其用作:

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
    {    

        [self keyBoradRemove];

     }

- (void) keyBoradRemove 
{
    searchBar.text = @"";
    [searchBar resignFirstResponder];

}

这里searchBar是UISearchBar的一个实例。

UISearchBar *searchBar;

希望它对你有所帮助。

答案 3 :(得分:0)

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

}

答案 4 :(得分:0)

使用搜索栏代理作为

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

答案 5 :(得分:0)

  

在点击键盘搜索按钮

时使用此代码隐藏键盘
- (void)searchBarTextDidBeginEditing:(UISearchBar *) bar
{
    UITextField *searchBarTextField = nil;
    NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ? bar.subviews : [[bar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
    if ([subview isKindOfClass:[UITextField class]])
    {
        searchBarTextField = (UITextField *)subview;
        break;
    }
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}

答案 6 :(得分:0)

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

   }

但在此之前,请检查您是否已正确设置搜索栏代理。

答案 7 :(得分:0)

对于swift 1.2你可以在外面触摸或点击搜索,在这两种情况下键盘都会隐藏

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    self.view.endEditing(true)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
    }