Timer decrement gets varied for each page load when using back button

霸气de小男生 提交于 2019-12-12 03:06:16

问题


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;
}  


来源:https://stackoverflow.com/questions/8641849/timer-decrement-gets-varied-for-each-page-load-when-using-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!