Timer decrement gets varied for each page load whe

2019-08-16 13:19发布

问题:

In my previous question Timer decrement gets varied for each page load I got the code working well when I used "Invalidate" and "Release". But when i use a back button the same problem repeats. And i need a stop option too..

-(IBAction)back:(id)sender
{

   [self dismissModalViewControllerAnimated:YES];
 }


 -(IBAction)stop:(id)sender
 {
[theTimer invalidate];
[theTimer release];
}

this is the code i used here but not working well.

回答1:

Try using it in this way..

if ([theTimer isValid]) {
    [theTimer invalidate];
    theTimer=nil;
}

And make sure your method is been called.



回答2:

whenever we use timer we must to remember to invalidate timer. when you press back button, your view controller hasbeen change but your timer is still working continue so at back button press you must invalidate timer first. and when you alloc timer first check timer is exist or not, if timer is exist you must invalidate timer first.

if(timer)
{
   [timer invalidate];
    timer = nil;
}