iPhone开发之NSTimer

每隔段时间执行相应的操作:

//时间间隔,每隔5秒执行handleMaxShowTimer
    NSTimeInterval timeInterval =5.0 ;
    //定时器
    NSTimer   *showTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                                            target:self
                                                          selector:@selector(handleMaxShowTimer:)
                                                          userInfo:nil
                                                           repeats:YES];

如果只想执行一次,则设置repeats为NO。

//触发事件
-(void)handleMaxShowTimer:(NSTimer *)theTimer
{
  //code here    
    
}

NSTimer初始化后,self的retainCount加1。

那么,我们需要在释放这个类之前,执行[timer invalidate];否则,不会执行该类的dealloc方法。
 
原文地址:https://www.cnblogs.com/foxmin/p/2444796.html