iOS:在用户完成输入几秒钟后调用函数 [英] iOS: Call function after a few seconds the user did finish typing

查看:32
本文介绍了iOS:在用户完成输入几秒钟后调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 UISearchBar 函数,它的调用方式与 App Store 中的搜索类似:在用户完成输入几秒钟后 (NSTimer) 执行该函数 (NSURLConnection),并且在此期间仅执行一次.

I'd like to create an UISearchBar function, which gets called like the search in the App Store: Execute the function (NSURLConnection) after a few seconds (NSTimer) the user did finish typing and just one time for this period.

有人有想法吗?

编辑

    self.currentTaskID = self.currentTaskID + 1;
    NSInteger taskID = self.currentTaskID;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), queue, ^{
        if (taskID == self.currentTaskID)
        {
            NSMutableURLRequest *request_CH3 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://website.com/search.php?term=%@", replaceWhiteSpace]]
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                           timeoutInterval:30.0];

            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

            authConnection_CH3 = [[NSURLConnection alloc] initWithRequest:request_CH3 delegate:self];
        }
    });

推荐答案

我最近做了一个类似问题的回答,你可以找到它 这里

I recently made an answer for similar question, you can find it here

基本上,您可以延迟搜索,如果在指定时间内有新搜索,您可以取消任何先前安排的搜索.

Basically you can queue your searches with delay and dismiss any previously scheduled search if new comes in within specified period.

这篇关于iOS:在用户完成输入几秒钟后调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆