views:

92

answers:

3

Hello all ,

I'd like to create some sort of timer that will take the time between firing different kind of functions but I'm having no such luck so far. I assumed I should use the NSDate object and I've done this so far.

I've created several functions

-(void)startTime:(id)sender
{
    starttime = [NSDate date];
    **[starttime retain];**   
    NSLog(@"show me the starttime %f", starttime);
}

-(void)endTime:(id)sender
{
    endtime = [NSDate date];
    **[endtime retain];**
    NSLog(@"show me the endtime %f", endtime);
}


-(void)timeInterval:(id)sender
{
    NSTimeInterval interval = [ starttime timeIntervalSinceDate:endtime]; 
    NSLog(@"let me see the timeinterval between now and then %f", interval);
}

For instance when I press a playbutton which will play a movie I will place the starttime function

-(void)playMovie:(id)sender { [self startTime:(id)sender]; [self callMovie]; }

And when I press a stopbutton for the movie the stopfunction will be called as will the timeInterval function

-(void)stopMovie:(id)sender
{
[self endTime:(id)sender]; 
[self myMovie];

[self timeInterval:(id)sender];
}

However it doesn't seem to work. If i press the stopbutton after 3 seconds I would assume my function timeInterval would give an output of 3 seconds. Unfortunately to no avail. Anyone knows what's going on and what I'm doing wrong?

EDIT:

thanks for the responses. I made some adjustments in my code snippets.

However I seem to have made a crucial error. I wasn't retaining endtime and starttime variables and this seems to have solved my case.

+1  A: 

A simple program using your example timing functions and sleep 3 yields -3.000087

Alex Brown
A: 

What happens when you press the stopbutton? If nothing happens, check your IBOutlets, the action is probably not hooked up to the method.

Jordan
A: 
-(IBAction)stopMovie:(id)sender {
    [self endTime:(id)sender]; 
    [self myMovie];
    [self timeInterval:(id)sender];
}

Shouldn't you be calling endTime instead of stopTime?

mahboudz
true just a typo, I corrected it.
jovany