How do I manage my NSTimer in an iPhone app in a tab?

I have a tab-based application with three tabs, one of which is a working clock. To animate the clock user interface, I use NSTimer, which fires once per second. If the user switches and exits from different tabs, how can I control this timer?

  • Can I use a scheduledTimerWithTimeIntervaltimer to create and schedule the first time I launch the application and let it die when the application closes? Or should I maintain an instance variable for a timer in the rootview controller for the clock tab and use addTimer:forMode:and invalidate?
  • If the stairs, how often should I add and cancel a timer? In my methods viewWillAppearand viewWillDisappearfor the rootview controller, the clock tabs? Somewhere else?

Thank you very much for your wisdom!

+3
source share
2 answers

I would do the following:

if your time zone appears → set the clock to the current time, then start the timer, which updates the clock every second:

clockRefreshTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector:@selector(updateClockView) userInfo:nil repeats:YES]

if the watch is about to disappear, an invalid timer

[clockRefreshTimer invalidate];

This way you are not making unnecessary updates.

hope i can help
sam

+1
source

@samsam, : , 1 , . . , , , , .

, ( , ), , , , , , , , .

+2

Source: https://habr.com/ru/post/1760450/


All Articles