PDA

View Full Version : NSTimer for Chronometer




powwowath
Dec 21, 2008, 04:19 AM
Hi again mates,

it may be a newbie question, but,.. I'm trying add a chronometer to my app and I'm looking NSTimer to create it. Is it a good idea?

thnks!



xsmasher
Dec 21, 2008, 05:18 PM
Hi again mates,

it may be a newbie question, but,.. I'm trying add a chronometer to my app and I'm looking NSTimer to create it. Is it a good idea?

thnks!

Yes and no - yes, you'll want an NSTimer that updates every second (or more often) to update your display of the elapsed time.

However, it may not get called exactly on time every time - so if you just use the timer to add +1 to the number of seconds elapsed, it will probably get out of sync and become innacurate. So you will also want to look at NSDate to figure out elapsed time.

You'll probably want something like this:

(1) create a date objects
(2) set up an nstimer to fire every second
(3) in the method called by the nstimer, check how much time has elapsed from the date until now.
(4) update the display with the amount of time elapsed.

powwowath
Dec 22, 2008, 01:44 AM
Thanks xsmasher!! I'll try it.