Using a NSTimer for HH:MM:SS? - xcode

How can I change this code so it has HH:MM:SS (hours, minutes, seconds)?
Do I need to add code in .h or .m so I known which one?
At the moment it goes up like 1, 2, 3, 4 etc.
Just to let you known I'm bait of a amateur would you copy and past so I known what you mean.
.h
#interface FirstViewController : UIViewController {
IBOutlet UILabel *time;
NSTimer *myticker;
//declare baseDate
NSDate* baseDate;
}
-(IBAction)stop;
-(IBAction)reset;
#end
.m
#import "FirstViewController.h"
#implementation FirstViewController
-(IBAction)start {
[myticker invalidate];
baseDate = [NSDate date];
myticker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)stop;{
[myticker invalidate];
myticker = nil;
}
-(IBAction)reset;{
time.text = #"00:00:00";
}
-(void)showActivity {
NSTimeInterval interval = [baseDate timeIntervalSinceNow];
NSUInteger seconds = ABS((int)interval);
NSUInteger minutes = seconds/60;
NSUInteger hours = minutes/60;
time.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes%60, seconds%60];
}

First, declare baseDate variable in your FirstViewController.h, like this:
#interface FirstViewController : UIViewController {
IBOutlet UILabel *time;
NSTimer *myticker;
//declare baseDate
NSDate* baseDate;
}
Then, in the FirstViewController.m start method add baseDate = [NSDate date] like this:
-(IBAction)start {
[myticker invalidate];
baseDate = [NSDate date];
myticker = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
}
After that, change your showActivity method to look like this:
-(void)showActivity {
NSTimeInterval interval = [baseDate timeIntervalSinceNow];
double intpart;
double fractional = modf(interval, &intpart);
NSUInteger hundredth = ABS((int)(fractional*100));
NSUInteger seconds = ABS((int)interval);
NSUInteger minutes = seconds/60;
NSUInteger hours = minutes/60;
time.text = [NSString stringWithFormat:#"%02d:%02d:%02d:%02d", hours, minutes%60, seconds%60, hundredth];
}
Also note, that you will have to change the interval value for your timer, otherwise your label will only be updated once a second.

Related

Pause and Resume Time Xcode

I know that there is no way to pause a timer using NSTimer. I know i have to store the date and call it again. I have tried every solution and i cannot find any that have successfully worked. So my question is, does anyone know how to pause and resume?
This worked good for me.
Created 2 UIButtons ('startButton' & 'resetButton'), a UILabel to display time ('timeLabel'), NSTimeInterval ('pauseTimeInterval'), NSTimer (stopWatchTimer') and used the following code:
NSDate *pauseStart, *previousFireDate, *startDate;
NSTimeInterval pauseTimeInterval;
NSTimer *stopWatchTimer;
-(void)pauseTimer{
pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
previousFireDate = [self fireDate];
[self setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[self setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
}
-(IBAction)startTime:(id)sender {
//start timer
if ([startButton.titleLabel.text isEqualToString:#"Start"] && (![self.stopWatchTimer isValid]) && ([timeLabel.text isEqualToString:#"00:00:00"]))
{
[startButton setTitle:#"Stop" forState:UIControlStateNormal];
startDate = [NSDate date];
startDate = [startDate dateByAddingTimeInterval:((-1)*(pauseTimeInterval))];
self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
resetButton.hidden = YES;
}
//pause timer
else if (([self.stopWatchTimer isValid]) && ([startButton.titleLabel.text isEqualToString:#"Stop"]))
{
[startButton setTitle:#"Resume" forState:UIControlStateNormal];
[self.stopWatchTimer pauseTimer];
resetButton.hidden = NO;
}
//resume timer
else {
[startButton setTitle:#"Stop" forState:UIControlStateNormal];
startDate = [NSDate date];
startDate = [startDate dateByAddingTimeInterval:((-1)*(pauseTimeInterval))];
[self.stopWatchTimer resumeTimer];
resetButton.hidden = YES;
}
}
-(IBAction)resetTime:(id)sender {
[self.stopWatchTimer invalidate];
self.stopWatchTimer = nil;
self.timeLabel.text = #"00:00:00";
[startButton setTitle:#"Start" forState:UIControlStateNormal];
pauseTimeInterval = 0.0;
resetButton.hidden = YES;
}
-(void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
self.timeLabel.text = timeString;
pauseTimeInterval = timeInterval;
}

Xcode How to stop a timer Label with an if statement when a UITextField has the same text as a UILabel

I have been trying to do this on an app for a long time but I cannot get it to work, please could someone help me and post some code on how I would do it. What I need to happen is when the text in the UITextField equal the UILabel. Thanks
h. viewcontroller
#interface AlphabetSceneViewController : UIViewController {
UILabel *stopWatchLabel;
NSTimer *stopWatchTimer;
NSDate *startDate;
IBOutlet UILabel *wordToType;
IBOutlet UITextField *wordTyped;
and the m. view controller
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
}
- (IBAction)onStartPressed:(id)sender {
startDate = [NSDate date];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
}
- (IBAction)onStopPressed:(id)sender {
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
if ([wordToType.text isEqualToString:#"stop"]) {
}
}
Help much appreciated!
I figured out how to do it, I'll put up the code for anyone that wanted to know. It's only code for the .m file and the alert view is just a bit of extra code on the end to make it show an alert when with the time that the user had when the timer stops.
- (IBAction)stopTimer:(id)sender {
if([wordTyped.text isEqualToString:wordToType.text]){
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Nice Work" message:[NSString stringWithFormat:#"Good Job! You're time to type the alphabet was %#. Let's see if you can do better...", stopWatchLabel.text]
delegate:self cancelButtonTitle:#"Main Menu" otherButtonTitles: nil];
[alert show];
}
}

Using NSDate and NSDate components to calc time between 2 dates (Max 6 days, 23 hrs, 59 min, 59sec in the future)

UPDATE: This is the working example.
First we create a class to hold weekday, hr, min, and sec:
myClass.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface myClass : NSObject {
NSString *weekday;
NSInteger hour;
NSInteger minute;
NSInteger second;
}
#property (nonatomic, strong) NSString *weekday;
#property (nonatomic, assign) NSInteger hour;
#property (nonatomic, assign) NSInteger minute;
#property (nonatomic, assign) NSInteger second;
#end
myClass.m
#import "myClass.h"
#implementation myClass
#synthesize weekday, hour, minute, second;
#end
Next, we need to create an instance of myClass that holds our date info.
Add this to ViewController.h:
#property (nonatomic, strong) NSMutableArray *myArray;
This code goes in ViewController.m wherever you'd like:
myArray = [[NSMutableArray alloc] init];
//Setup an instance of myClass
myClass *c = [[myClass alloc] init];
[c setWeekday:#"Monday"];
[c setHour:13];
[c setMinute:0];
[c setSecond:0];
[myArray addObject:c];
Next we need to figure out how far in the future our event is. Thanks to rdelmar we have the code to do this, his answer is below
//Create a func that returns an NSDate. It requires that the Weekday, HR, Min and Secs are passed into it.
-(NSDate *)getNextDateOn:(NSString *)weekday atHour:(NSInteger)hour minute:(NSInteger)mins second:(NSInteger)secs {
//Setup an array of weekdays to compare to the imported (NSString *)weekday
NSArray *array = [NSArray arrayWithObjects:#"Sunday",#"Monday",#"Tuesday",#"Wednesday",#"Thursday",#"Friday",#"Saturday",nil];
NSInteger weekdayNumber = [array indexOfObject:[weekday capitalizedString]] + 1;
//This code finds how many days in the future the imported (NSString *)weekday is
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *nowComps = [cal components:NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now];
NSInteger daysForward = (weekdayNumber - nowComps.weekday + 7) % 7;
//Lastly, create an NSDate called eventDate that consists of the
NSDateComponents *eventComps = [[NSDateComponents alloc] init];
[eventComps setDay:daysForward];
[eventComps setHour: hour - nowComps.hour];
[eventComps setMinute: mins - nowComps.minute];
[eventComps setSecond: secs - nowComps.second];
eventDate = [cal dateByAddingComponents:eventComps toDate:now options:0];
return eventDate;
}
Here, we take the newly created eventDate and use it to create our event in iCal:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Move your car!";
event.startDate = eventDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:60.0f * 60.0f sinceDate:event.startDate]; //1 hr long
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -30.0f]]; //30 min before
//eventLoc was created using CLGeocoder and the method reverseGeocodeLocation:
//The location is not necessary to create an event but if you'd like the code, ask and i'll post it.
[event setLocation:eventLoc];
[event setNotes:#"This event was set by me. ;P"];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"Event Set");
I hope this helps someone as much as it's helped me.
:END OF UPDATE:
I've read through the documentation of NSDate hoping to find an easy way to find "the next upcoming Mon 1:00PM".
For instance, lets say the bakery is open 1 day a week (Thurs) from 9am - 6pm... If it is now Thurs, 8am, I want to get an NSDate for 1hr from now. If today were Thurs # 7pm, I'd want the NSDate for next Thurs at 9am.
I plan on making an event in iCal (tests have been successful) but the trouble is in calculating the event time.
Can you point me towards a good explanation of NSDate or help me figure out how to calculate the NSDate's I'm looking for?
I'm looking to fix this code:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Bakery's Open!";
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
To find a date like this requires using NSDateComponents and NSCalendar. The main task, I think, is to figure out how many days in the future your target date is. To do this you need to get the weekday date component of the current date and calculate how many days it is to the weekday you're looking for. I think the following code should get you close:
-(NSDate *)getNextDateOn:(NSString *)weekday atHour:(NSInteger)hour minute:(NSInteger)mins second:(NSInteger)secs {
NSArray *array = [NSArray arrayWithObjects:#"Sunday",#"Monday",#"Tuesday",#"Wednesday",#"Thursday",#"Friday",#"Saturday",nil];
NSInteger weekdayNumber = [array indexOfObject:[weekday capitalizedString]] + 1;
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *nowComps = [cal components:NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now];
NSInteger daysForward = (weekdayNumber - nowComps.weekday + 7) % 7;
NSDateComponents *eventComps = [[NSDateComponents alloc] init];
[eventComps setDay:daysForward];
[eventComps setHour: hour - nowComps.hour];
[eventComps setMinute: mins - nowComps.minute];
[eventComps setSecond: secs - nowComps.second];
NSDate *eventDate = [cal dateByAddingComponents:eventComps toDate:now options:0];
[eventComps release];
return eventDate;
}

Using A NSTimer for MM:SS:HS?

I have created a NSTimer In Xcode 4.2 and it works but i get this one problem.
here is my project in the simulator
when i press start it starts and when i press stop it stops and when its stopped it will reset
but when it starts and i press reset when it is going nothing happens it don't reset when started basically you have to stop then reset is the ways and this or do i need to add code any where heres a copy of my code.
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController {
IBOutlet UILabel *time;
NSTimer *myticker;
//declare baseDate
NSDate* baseDate;
}
-(IBAction)stop;
-(IBAction)reset;
#end
heres my implementation
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize baseDate;
-(IBAction)start {
[myticker invalidate];
self.baseDate = [NSDate date];
myticker = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)stop;{
[myticker invalidate];
myticker = nil;
}
-(IBAction)reset {
self.baseDate = [NSDate date];
time.text = #"00:00:0";
}
-(void)showActivity {
NSTimeInterval interval = [baseDate timeIntervalSinceNow];
double intpart;
double fractional = modf(interval, &intpart);
NSUInteger hundredth = ABS((int)(fractional*10));
NSUInteger seconds = ABS((int)interval);
NSUInteger minutes = seconds/60;
time.text = [NSString stringWithFormat:#"%02d:%02d:%01d", minutes%60, seconds%60, hundredth];
}
I Really Appreciate It. Thanks.
First, the above should crash with EXC_BAD_ACCESS when it reaches showActivity since baseDate is not being retained in the start method. [NSDate date] returns an autoreleased object so baseDate will have an invalid reference after the start method.
I suggest changing baseDate to a retain property and then setting it in start using self.:
//.h
#property (nonatomic, retain) NSDate *baseDate;
//.m
#synthesize baseDate;
-(IBAction)start {
[myticker invalidate];
self.baseDate = [NSDate date];
myticker = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
}
To fix the reset problem, note that the showActivity method takes the current value of baseDate to calculate the elapsed time and then sets the time label to display it formatted.
In the start method, you set the baseDate to the current time (you don't set time.text) and then start the timer. The showActivity method will then keep firing and set time.text.
In the reset method, you want the timer to start showing the elapsed time since the moment reset is pressed. The timer is already running so you don't need to re-start it. Setting the time label text doesn't work because when the already-running timer fires again, it will calculate the elapsed time from baseDate which is still the original start time and then set time.text based on that. So instead of setting time.text, set the baseDate:
-(IBAction)reset {
self.baseDate = [NSDate date];
}

NSTimer Troubles

I am trying to run the code below but it keeps locking up my simulator after the "Tick" is written to the console. It never outputs "Tock" so my guess is that it has to do with the line "NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];" The IBactions are activated by buttons. timer and startTime are defined in the .h as NSTimer and NSDate respectively.
Can anyone tell me what I am doing wrong?
code:
- (IBAction)startStopwatch:(id)sender
{
startTime = [NSDate date];
NSLog(#"%#", startTime);
timer = [NSTimer scheduledTimerWithTimeInterval:1 //0.02
target:self
selector:#selector(tick:)
userInfo:nil
repeats:YES];
}
- (IBAction)stopStopwatch:(id)sender
{
[timer invalidate];
timer = nil;
}
- (void)tick:(NSTimer *)theTimer
{
NSLog(#"Tick!");
NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];
NSLog(#"Tock!");
NSLog(#"Delta: %d", elapsedTime);
}
I have the following in the .h:
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
NSTimer *timer;
NSDate *startTime;
}
- (IBAction)startStopwatch:(id)sender;
- (IBAction)stopStopwatch:(id)sender;
- (void)tick:(NSTimer *)theTimer;
#property(nonatomic, retain) NSTimer *timer;
#property(nonatomic, retain) NSDate *startTime;
#end
Where you have:
startTime = [NSDate date];
You need:
startTime = [[NSDate date] retain];
Anything that is created with out an alloc, new, init will be auto-released (rule of thumb). So what is happening is you are creating the NSDate, assigning it to startTime, it's getting auto-released (destroyed), then you are trying to call timeIntervalSinceNow on an object that was fully released so it blows up.
Adding the retain increased the retain count so it still sticks around after the auto-release. Don't forget to manually release it when you're done with it though!
To take advantage of the #property you need to do:
self.startTime = [NSDate date]

Resources