我有以下的UISearchBar代碼:如何檢測輸入中的UISearchBar/UITextField的暫停?
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString* endpoint =[NSString stringWithFormat:@"http://www.someurl/",
[searchText stringByReplacingOccurrencesOfString:@" " withString:@"+"]];
NSURL* url = [NSURL URLWithString:endpoint];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(searchResultsFetcher:finishedWithData:error:)];
}
我想給在輸入暫停後該請求,並復位定時器每次一個字符被擊中。我怎樣才能做到這一點?
比NSTimer更優雅的解決方案,我想。可能會導致問題取決於runloop和用戶輸入或請求,也許?嗯,不再是一個計時器,雖然 – RSully
不錯!這裏有另一個詳細評論的例子:http://benedictcohen.co.uk/blog/archives/157 – ma11hew28
不起作用,即使我們取消,選擇器也會重複調用(swift 3) – Saif