Timer app

Discussion in 'iOS Programming' started by arnieterm, Jan 28, 2009.

  1. arnieterm macrumors regular

    Aug 28, 2008
    In a navigation controller based iphone app with two view controllers. I have to make use of a timer. That timer should be accessible from both view controllers. Should this timer be created in app delegate so that both view controllers can access it
    The first view controller takes some entries in text fields and starts a timer based on these. The next view controller shows the time elapsed since the start of the timer. This means the UILabel on 2nd view controller needs to be updated after some pre-defined time interval based on entries from first controller.
    Can we use NSTimer for iphone? If not then what other option can be used?

  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Yes, you can use NSTimer, as demonstrated in the BonjourWeb, HeadsUpUI, SpeakHere, TableViewSuite and WiTap sample apps.
    Either add it to the appDelegate or create a new class that both viewControllers can access.
  3. arnieterm thread starter macrumors regular

    Aug 28, 2008
    I have created an NSTimer within my view controller that has labels for updating the hour, minute and seconds. I need to repeat the timer after every 1 second as the app is similar to stop watch. Should I use NSDate or Hours, Minutes, Seconds [NSInteger] for handling the time as I am looking for performance viewpoint also. When
    Also I need to pause the NSTimer at the click of a button and then to resume it at another click from where it last starts. I see using invalidate will stop the timer, right?
  4. jnic macrumors 6502a

    Oct 24, 2008
    One second is long enough that performance won't be an issue, so use whatever you're most comfortable with.

    Right. Class ref here.
  5. arnieterm thread starter macrumors regular

    Aug 28, 2008
    NSTimer's invalidate method is stopping the timer from executing but I am looking for a way to temprarily pausing at a click of a button. Then I want to resume the timer at the click of another button
  6. WhiteRabbit macrumors newbie

    Jan 11, 2005
    I belive you could use the fireDate method of NSTimer to extract the fire date to an NSTimeInterval object using timeIntervalSinceNow. Then use set setFireDate on your timer to pause your timer with [NSDate distantFuture] (your timer will keep going, but to an indefinite future time).

    Finally, to resume, use setFireDate on your timer object once again, passing it your recently created NSTimeInterval object.
  7. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    You could just leave the timer running, check a pause flag inside the time handler, and not draw when paused.

Share This Page