Shimon Wiener
Shimon Wiener

Reputation: 1180

searchBar textDidChange is called two times when search field taped in UISearchBar

I have a UISearchBar created programmatically in a function called from viewDidLoad in this way

//***************************************
//***  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];
}

when i tap on the search field my [searchBar textDidChange] delegate is called two times and i cant figur it out, It happend only on the first time when user tap the search field, is it normal response?. hear is the code

-(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];
}

I'm working in xCode 4.3.1 i know i have unexplained code in this function but even when i remark it it still called two times, trying to solve it but cant fined Y be happy to get help

Upvotes: 1

Views: 3425

Answers (1)

rakeshNS
rakeshNS

Reputation: 4257

Use

 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 

instead of

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

Upvotes: 1

Related Questions