NSTimer doesn't stop with invalidate

后端 未结 2 763
予麋鹿
予麋鹿 2020-12-19 10:25

I add timer like this

tim=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatTim) userInfo:nil repeats:YES];
[[NSRunLoop mainRun         


        
2条回答
  •  粉色の甜心
    2020-12-19 10:56

    You have more than one timer running . Try this:

    -(void)startTimer{
        [self.myTimer invalidate]; // kill old timer
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(doSomething) userInfo:nil repeats:YES];
    }
    
    -(void)stopTimer{
        [self.myTimer invalidate];  
        self.myTimer=nil; //set pointer to nil 
    }
    

提交回复
热议问题