我有一个UISearchBar,它是用这种方式在viewDidLoad调用的函数中以编程方式创建的
//***************************************
//*** 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]代理被调用了两次,我无法计算出来,这只发生在用户第一次点击搜索字段的时候,这是正常响应吗hear是代码
-(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很高兴得到帮助
使用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
而不是
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText