2013-05-02 90 views
4

我正在使用UISearchBar搜索列表,但當我點擊鍵盤上的搜索按鈕 時,鍵盤不會隱藏。
請幫我解決這個問題。
這裏是我的代碼無法隱藏鍵盤單擊搜索按鈕在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]; 

} 
+0

您的問題添加更多描述你的代碼的例子,讓人們將能夠幫助你。 – 2013-05-02 13:35:40

回答

0

首先設置searchbar.delegate = self然後在- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar呼叫[searchbar resignFirstRespnder]

好運

+0

仍然我使用相同的代碼行,但它不適合我。 – 2013-05-02 12:36:20

+0

我編輯了答案,以滿足您的問題 – 2013-05-02 12:38:13

+0

下面的代碼我是using-(無效),在searchBarSearchButtonClicked:(*的UISearchBar)theSearchBar { \t \t [theSearchBar resignFirstResponder]; } – 2013-05-02 12:38:50

4

,就把這行的代碼,然後再試一次,

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

(或) EDIT

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

我應該在哪裏使用這行代碼? – 2013-05-02 12:45:00

+0

不,它不適用於我 – 2013-05-02 12:49:22

+0

現在檢查它是否正常工作將其置於searchBarTextDidEndEditing方法中。 – Balu 2013-05-02 12:50:46

0

您可以使用下面的代理,當搜索欄搜索按鈕被點擊時調用。

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

    [theSearchBar resignFirstResponder]; 

} 

或者以此爲:

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

     [self keyBoradRemove]; 

    } 

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

} 

這裏搜索欄是的UISearchBar的一個實例。

UISearchBar *searchBar; 

希望它可以幫助你。

+0

Nishant bhai ye bhi kam nahi kar raha hai.please help – 2013-05-02 12:52:48

+0

它也不工作 – 2013-05-02 13:09:40

+0

@dhiraj它應該工作。我假設你在代碼中做了錯誤的事情。 Plz再次檢查您的代碼。 – 2013-05-02 13:23:46

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

} 
0

使用搜索欄委託作爲

 -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1 
    { 
     [searchBar1 resignFirstResponder]; 
    } 
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; 
} 
0
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
    { 
     [searchBar resignFirstResponder]; 

    } 

的搜索按鈕的點擊,但在此之前,親切檢查你是否有設置e搜索欄正確授權。

0

爲SWIFT 1.2,你可以觸摸外面或點擊搜索,在這兩種情況下,鍵盤會隱藏

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