2012-03-20 104 views
1

我有這樣一個從viewDidLoad中調用的函數編程方式創建一個UISearchBar錄音搜索欄textDidChange被調用兩次時,搜索領域的UISearchBar

//*************************************** 
//*** Build the UINavigationBar  * 
//*************************************** 
- (void) buildBar { 

    search = [[UISearchBar alloc] init]; 
    [search sizeToFit]; 
    search.delegate = self; 
    [[search.subviews objectAtIndex:0] removeFromSuperview]; 

    self.navigationItem.titleView = search; 

    tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 110.0f, 44.01f)]; 
    tools.barStyle = UIBarStyleBlackTranslucent; 
    if(tools.subviews.count >0) 
    { 
     [[[tools subviews] objectAtIndex:0] removeFromSuperview]; 
    } 
    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2]; 
    [buttons addObject:[self editButtonItem]]; 
    bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(activateActions:)]; 

    bi.style = UIBarButtonItemStyleBordered; 

    [buttons addObject:bi]; 
    [bi release]; 

    [tools setItems:buttons animated:NO]; 

    [buttons release]; 

    UIBarButtonItem *tollbarButtons = [[UIBarButtonItem alloc] initWithCustomView:tools]; 

    self.navigationItem.rightBarButtonItem = tollbarButtons; 

    [tollbarButtons release]; 

// [tools release]; 
// [search release]; 
} 

當我在搜索欄我的[搜索欄textDidChange]委託挖掘被稱爲兩次,我不能找出它,它只發生在用戶第一次點擊搜索字段時,它是否正常響應? 聽到的是代碼

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
     if([searchText length] == 0 && firstTimeSearch == NO){ 
      firstTimeSearch = YES; 
      [filterCalls removeAllObjects]; 
      [filterCalls addObjectsFromArray:allCalls]; 

/*   [searchBar performSelector: @selector(resignFirstResponder) 
          withObject: nil 
          afterDelay: 0.1];*/ 
    } 
    else{ 
      firstTimeSearch = NO; 
      [filterCalls removeAllObjects]; 
     [filteredCallsDetails removeAllObjects]; 

//   
      int i = 0; 
      for(NSDictionary *call in callsDetails){ 

//    NSNumber *callId = [call objectForKey:@"CallID"]; 

       generalUtils *gu = [[generalUtils alloc]init]; 
       NSArray *callDetail = [[NSArray alloc]initWithArray:[gu getFilteredCallDetails:i :filterCalls :filteredCallsDetails]]; 

       for (NSDictionary *answer in callDetail) { 
        NSRange r = [[answer objectForKey:@"answer"] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
        if(r.location != NSNotFound){ 

         [filterCalls addObject:call]; 
         [filteredCallsDetails addObject:callDetail];     } 

       } 

       i++; 
     } 
    } 
     [self.tableView reloadData]; 
} 

我在Xcode 4.3.1 我知道我有不明原因的代碼在此功能,但即使我此話它仍然叫做兩次,試圖解決它,但不能工作Ÿ罰款是 高興能得到的幫助

回答

1

使用

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 

代替

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
+0

好吧,那麼我將如何從搜索字段中獲取文本? – 2012-03-20 16:54:30

+0

@ShimonWiener searchBar.text將保存您的搜索文本 – rakeshNS 2012-03-20 17:02:06

+0

感謝您的答案,但我需要執行搜索我的數據陣列,因爲用戶敲擊鍵盤,所以我需要實現「textDidChange」,你知道嗎?叫兩次? – 2012-03-20 17:10:39