Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

arnieterm

macrumors regular
Original poster
Aug 28, 2008
201
0
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?

Thanks
Arnieterm
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Can we use NSTimer for iphone? If not then what other option can be used?
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.
 

arnieterm

macrumors regular
Original poster
Aug 28, 2008
201
0
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?
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
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

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

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?

Right. Class ref here.
 

arnieterm

macrumors regular
Original poster
Aug 28, 2008
201
0
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
 

WhiteRabbit

macrumors newbie
Jan 11, 2005
26
0
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.