且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS:等到用户完成输入然后发送请求

更新时间:2023-11-20 23:32:10

在发送请求之前添加一个小延迟,然后在用户继续输入时取消该延迟请求

Add a small delay before sending the request, and then cancel that delayed request if the user continues typing

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];

    [self performSelector:@selector(sendSearchRequest) withObject:searchText afterDelay:0.1f];
}

您可能需要调整延迟时间。太长时间并且用户注意到有延迟,太短而你现在遇到同样的问题

you may need to adjust the delay time. too long and its noticeable to the user that there is a delay, too short and you have the same problem as now