How to Implement a Countup Timer

Discussion in 'iOS Programming' started by Stunner, Nov 10, 2009.

  1. Stunner macrumors newbie

    Sep 14, 2009
    Ey fellas, I am wondering how I would go about creating something in which the user taps a button, a timer starts counting(in seconds, if possible) and then the user taps the button again and it measures the amount of time that has passed. I have looked here: to no avail. I tried understanding what it said but it really just ended up convoluted things much more for me. If anyone could possibly provide an example of how this would be done, it would be greatly appreciated. Thanks in advance!
  2. Troglodyte macrumors member

    Jul 2, 2009
    When the button's pressed record the time using [NSDate date] and start an NSTimer that calls a method to update your counter, change the action method for the button to one for when the counter is to be stopped. In that method you use [NSDate date] again so you can work out the time elapsed and invaldiate the NSTimer.

  3. Stunner thread starter macrumors newbie

    Sep 14, 2009
    Ey, thanks for the response. Just a few questions though as I am relatively new to all of this. When I start NSTimer what happens exactly? A counter starts in the background or do I need to update the counter every run? And how would I go about changing the action method(as well as the button label) for the button? Is there any guide or tutorial you can link me to? Also, why would I call NSDate at all? Why not just use NSTimer alone? Thanks.
  4. Troglodyte macrumors member

    Jul 2, 2009
    You should actually read up on this rather than just asking here but anyway...

    You set up an NSTimer to call a certain method at a certain freqeuncy and say if you want it to do it once or to repeat. It doesn't do anything to the counter.
    You need these methods
    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
    - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
    Which UIButton inherits from UIControl
    Look at the UIButton documentation. It's pretty obvious.
    No idea.
    NSTimer doesn't store the time nor how long it's been in existence. You need to get the time the counter was started with [NSDate date] and keep it in a retained variable so you can work out the duration later.
  5. Stunner thread starter macrumors newbie

    Sep 14, 2009
    Thanks a lot for the thorough response. I do look around on google as well as the documentation before posting up questions to this forum(believe it or not) but the documentation isn't always very clear and can be hard to sift through. All I need is a starting point so that I can drill down from there, which is exactly what you have provided me with, and thank you for that.

    To get the differences between button taps I just ended up using:
    [date2 timeIntervalSinceDate:date1]
    Here is a link to the page I found it on(for those who are interested):

    I think that is a simpler way than you had suggested, and it gives me what I want. I tried reading up on NSTimer methods but the lack of examples in the documentation is a real killer... Anyways thanks for all your help. :)

Share This Page