簡體   English   中英

iOS:幾秒鍾后用戶確實輸入了呼叫功能

[英]iOS: Call function after a few seconds the user did finish typing

我想創建一個UISearchBar函數,該函數的調用類似於在App Store中的搜索:在用戶確實完成鍵入幾秒鍾(NSTimer)之后執行該函數(NSURLConnection),並且在此期間僅執行一次。

有人有主意嗎?

編輯

    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];
        }
    });

我最近對類似問題做了回答,您可以在這里找到

基本上,您可以將搜索延遲入隊,如果在指定時間內出現了新搜索,則可以取消以前計划的搜索。

而不是使用計時器來執行此操作,而是實現UISearchBarsearchBarTextDidEndEditingsearchBar:textDidChange的委托函數,然后在對用戶輸入的字符數感到滿意時,將啟動NSURLConnection請求

在您要調用搜索功能的功能中(按您的情況單擊后)

實現這個:

[timer invalidate];
    timer = nil;
    timer = [NSTimer scheduledTimerWithTimeInterval: 3.0 target: self selector: @selector(thefunctionyouwanttocall) userInfo: nil repeats: NO];

這將在點擊事件后3秒鍾調用您的函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM