开发者

UITextfield and webservice

I have one query regarding the 开发者_如何学C(UITextfield and Webservice) for example

When i start writing text in UITextfield. Make sure, the app does not query the server(Webservice) for each letter typed. It should wait till I make a pause and then send the request.

Is this possible to when using of UITextField?

Thanks in advance


You can do that using performSelector:withObject:afterDelay: and cancelPreviousPerformRequestsWithTarget: methods. Here's sample code - requestAction: gets called after user makes 2sec pause in editing text field.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // Cancel previously registered perform request 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(requestAction) object:nil];
    // schedule a call with 2 sec delay
    [self performSelector:@selector(requestAction) withObject:nil afterDelay:2.0];

    return YES;
}

- (void) requestAction{
    NSLog(@"Request!");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜