UISearchBar检测用户何时停止类型并且不立即搜索,因此检测暂停 [英] UISearchBar detect when user stop type and don't search immediately so detect pause
问题描述
我正在搜索实现从网址检索信息的UISearchbar,并使用默认方法:
i'm searching implementing a UISearchbar that retrieve information from a url, and with the default method:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
我可以立即检测到文本更改并执行url的获取,但是这样,文本类型很慢,因为iPhone正在搜索url,所以我想在用户停止写入一段时间后开始获取url,所以我想要检测键入的暂停以刷新通过URL检索信息的表视图。我找到了这个请求的旧帖子,我尝试了一个解决方案,对我来说不起作用:
i can detect immediately when the text change and perform the fetch of url, but in this way, the text type is slow because the iPhone is searching the url, so i want start the fetching of the url when the user is stop writing for some second, so i want detect the pause of the typing to refresh the table view retrieving the information by the url. i have found a older post of this request and i have tried a solution that for me don't work:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(request:) object:searchText];
//.....
[self performSelector:@selector(request:) withObject:searchText afterDelay:1.5];
}
-(void)request:(NSString *)myString
{
NSLog(@"%@",myString);
}
当我输入请求方法时这种方式没有被调用但是当我停止输入它为我键入的每个字符调用,所以默认方法是相同的,我错了什么?或者实现不正确?
in this way when i'm typing the request method is not called but when i stop typing it's called for every character i type, so is the same of the default method, i wrong something? or the implementation isn't correct?
推荐答案
看起来提供的解决方案无效,因为searchText参数为 cancelPreviousPerformRequestsWithTarget:selector:object:
与前一次调用 performSelector:withObject:afterDelay:$ c $的searchText参数不匹配C>;因此,延迟搜索会在每次文本更改时排队,并且永远不会取消。
It looks like the provided solution isn't working because the "searchText" argument to cancelPreviousPerformRequestsWithTarget:selector:object:
doesn't match the "searchText" argument to the previous call to performSelector:withObject:afterDelay:
; so delayed searches get queued up every text change, and never cancelled.
您可以使用 NSTimer
来延迟调用您的搜索,并在文本更改时取消计时器:为您的对象提供 NSTimer
属性或字段;在 searchBar:textDidChange:
取消任何现有的计时器,然后创建并计划一个新的计时器。计时器的目标应该调用你的搜索方法。
You could use an NSTimer
to delay the invocation of your search, and cancel the timer whenever the text changes: Give your object an NSTimer
property or field; in searchBar:textDidChange:
cancel any existing timer, then create and schedule a new one. The target of the timer should call your search method.
类似的东西(在我的头顶):
Something like (off the top of my head):
// in your class' .h object fields
{
...
NSTimer *searchDelayer; // this will be a weak ref, but adding "[searchDelayer invalidate], searchDelayer=nil;" to dealloc wouldn't hurt
...
}
// in the .m
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[searchDelayer invalidate], searchDelayer=nil;
if (YES /* ...or whatever validity test you want to apply */)
searchDelayer = [NSTimer scheduledTimerWithTimeInterval:1.5
target:self
selector:@selector(doDelayedSearch:)
userInfo:searchText
repeats:NO];
}
-(void)doDelayedSearch:(NSTimer *)t
{
assert(t == searchDelayer);
[self request:searchDelayer.userInfo];
searchDelayer = nil; // important because the timer is about to release and dealloc itself
}
一些程序员可能会关于弱参考的人比我在这里更少骑士。
Some programmers might be less cavalier about weak references than I'm being here.
如果你设置使用cancelPreviousPerformRequestsWithTarget ......,那么你可以这样做:
Here's how you might do it if you were set on using cancelPreviousPerformRequestsWithTarget...:
// in your class' .h object fields
{
...
NSString *priorSearchText; // this will NOT be a weak ref, so adding "[priorSearchText release]" to dealloc is also required
...
}
// in the .m
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(request:)
object:priorSearchText];
[priorSearchText release], priorSearchText = [searchText retain];
if (YES /* ...or whatever validity test you want to apply */)
[self performSelector:@selector(request:)
withObject:searchText
afterDelay:1.5];
}
我认为我从未使用过cancelPreviousPerformRequestsWithTarget:... for真的,所以我不知道它是否隐藏任何意外。如果您遇到问题,请添加NSLogs,查找延迟搜索未被取消的时间。
I don't think I've ever used cancelPreviousPerformRequestsWithTarget:... for real, so I don't know if it hides any surprises. If you have trouble, add NSLogs, look for delayed searches not getting cancelled when they should.
这篇关于UISearchBar检测用户何时停止类型并且不立即搜索,因此检测暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!