Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Pause when user stop type in UISearchBar

I have the following UISearchbar code:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSTimer *myTimer;

    NSLog(@"Timer=%@",myTimer);
    if (myTimer)
    {
        if ([myTimer isValid])
        {
            [myTimer invalidate];
        }
        myTimer=nil;
    }
    myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(OnTextChange) userInfo:nil repeats:NO];

}

after writing above code my log shows Timer=null,

OnTextChange is method which fetches data from Url.

Thanks in advance !!

like image 639
Krunal Avatar asked Mar 24 '26 08:03

Krunal


1 Answers

Please write this line in .h file

NSTimer *myTimer;

and then do it in .m file

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    NSLog(@"Timer=%@",myTimer);
    if (myTimer)
    {
        if ([myTimer isValid])
        {
            [myTimer invalidate];
        }
        myTimer=nil;
    }
    myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(OnTextChange) userInfo:nil repeats:NO];

}
like image 121
Dharmbir Singh Avatar answered Mar 26 '26 01:03

Dharmbir Singh