当搜索字段在UISearchBar中录制时,searchBar textDidChange被调用两次

时间:2012-03-20 16:27:35

标签: ios uitableview uinavigationbar uisearchbar

我以这种方式从viewDidLoad调用的函数中以编程方式创建了一个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];
}

当我点击搜索字段时,我的[searchBar 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中工作 我知道我在这个函数中有无法解释的代码,但即使我说它仍然调用了两次,试图解决它,但不能罚款Y 很乐意得到帮助

1 个答案:

答案 0 :(得分:1)

使用

 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 

而不是

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