简体   繁体   中英

How to stop timer when enter to background?

I have one timer that will countdown the time to 0. timer stop fine when it reach to 0 and when clicking submit button but timer doesn't stop when go out the application and come back. I tried to create the timer in viewWillAppear and invalidate it in viewWillDisappear but timer still running after coming back from background. How to stop timer when enter to background? Any sample?

Thanks

You can use the :

- (void)applicationDidEnterBackground:(UIApplication *)application 

method of appDelegate class to stop your time . But for that you have to declare the timer inside the appDelegate class. Also you have to persist the counter when you will go to background .

Then you can use the viewWillAppear: method to restart the timer.

You can use NSNotificationCenter . Add observer for UIApplicationDidEnterBackgroundNotification and stop your timer in it. You won't have to create your timer in AppDelegate in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM