UISearchBar代碼:
- (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];
}
在日志中顯示Timer=null
,其中OnTextChange 是從URL提取數據的方法。
將這行代碼寫在 .h文件中:
NSTimer *myTimer;
然後在 .m文件中這樣:
- (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];
}