tags:

views:

119

answers:

1

How would I use a NSTimer to do a countdown or is that even possible ? I've tried a couple of things and got a few errors dealing with the numbers but wat I want to happen is when my timer gets activated it starts off with 5:00 and for each second It takes away I want a new number to appear on my label so....

if(Timer1 == 5:00) {

// label will display 5

}

For some reason this code gives me an error for using 5:00.. also I don't think it will even be called cause the timer doesn't activate the method until the time runs out so I'm kinda stuck :/ any help ?

+1  A: 

5:00 isn't a valid number in C or Objective-C.

The first thing your going to want to do is go read the intro to Objective-C tutorials at developer.apple.com and then, likely, followup with some kind of "programming the iPhone" book.

Your question demonstrates a pretty thorough lack of understanding. Nothing wrong with that -- just expect to have to do a bunch of studying/learning ahead of you.

Once you do that, you'll be able to understand this:

A repeating NSTimer is not guaranteed to fire every N minutes/seconds/hours/days (whatever your interval is). There is slop; considerable slop depending on what happens when the timer fires.

Thus, you want to create a periodic timer that fires however often you want to update the UI and then figure out exactly how much time has elapsed using a different mechanism.

Grab the start time -- [NSDate date] works well -- store it away somewhere and then use it to determine how much time has actually elapsed when the timer fires (by grabbing another [NSDate date] and subtracting the first from it to get the elapsed time).

bbum
Also note that when you do set up the time interval for the timer to fire, you probably want it to be less than one second. That way you won't have issues where your timer is suddenly ticking off 2 seconds in a jump to catch up, rather you'll just note you don't have to update anything and return early.
Jason Coco
With the additional caveat that the faster you configure your timer for, the more battery power you will consume.
bbum