Hi All,
In my app I want to display stop watch kind of thing i.e when the user starts the app a atop watch should start and stops when the user presses the stop button. I am not sure as to how to use the NSTimer in this case.
Hi All,
In my app I want to display stop watch kind of thing i.e when the user starts the app a atop watch should start and stops when the user presses the stop button. I am not sure as to how to use the NSTimer in this case.
Well, you know how to retrieve the start time ([NSDate date]
) when the user clicks the button, so you just need to periodically retrieve the current time and display the difference between the start time and the current time to the user.
An NSTimer
is used to periodically invoke a method. In that method you can just use [NSDate date]
to retrieve the current time, then use the -[NSDate timeIntervalSinceDate:]
method to figure out how much time has passed since the timer started. (That method will return the difference between the two dates in seconds, with sub-second precision. Then you just need to do some simple math to transform a number of raw seconds into a user-readable string (ie, 90 seconds => 1 min, 30 seconds) and display that in your UI.