当在 UISearchBar 中粘贴搜索字段时,searchBar textDidChange 被调用两次
Posted
技术标签:
【中文标题】当在 UISearchBar 中粘贴搜索字段时,searchBar textDidChange 被调用两次【英文标题】:searchBar textDidChange is called two times when search field taped in UISearchBar 【发布时间】:2012-03-20 16:27:35 【问题描述】:我有一个 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] 委托被调用了两次,我无法弄清楚,仅在用户第一次点击搜索字段时发生,这是正常响应吗? 听到是密码
-(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】:使用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
而不是
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
【讨论】:
好的,我将如何从搜索字段中获取文本? @ShimonWiener searchBar.text 将保存您的搜索文本 感谢您的回答,但我需要在用户点击键盘时对我的数据数组执行搜索,所以我需要实现“textDidChange”,你知道它调用两次的方式吗? 看这个链接,可能是这样的问题jomnius.blogspot.in/2010/07/… 抱歉,你邀请我聊天是在晚上。我睡着了。以上是关于当在 UISearchBar 中粘贴搜索字段时,searchBar textDidChange 被调用两次的主要内容,如果未能解决你的问题,请参考以下文章
字段为空时为 UISearchBar 启用“搜索”按钮? (IOS 7)