views:

88

answers:

2

how to make timer counter zero in this particular code when I click on the reset button the values must go to 00:00:00 and when i click on start button timer should be start from starting please help out

code here this in the .h file code

-(void)start
{
timer1 = [NSTimer scheduledTimerWithTimeInterval:(.01) target:self selector:@selector(timepassed) userInfo:nil repeats:YES];

}

this is in the .m file

-(void)timepassed
{

counter++;

     if (counter == 60)  { sec++; counter = 0; }
    if (sec == 60) { min++; sec = 0; }
    if (min == 60) { hr++; min = 0; }

NSString *l=[NSString stringWithFormat:@"%i:%i:%i",hr,min,sec];

[m setText:l];

}
A: 

If need a variable to control if timer is running or not, then your function would look like:

-(void)timepassed 
{
    if (isTimerRunning)
    {
        counter++;

        if (counter == 60)  { sec++; counter = 0; }
        if (sec == 60) { min++; sec = 0; }
        if (min == 60) { hr++; min = 0; }
    }
    NSString *l=[NSString stringWithFormat:@"%i:%i:%i",hr,min,sec];

    [m setText:l];
}

With "Start" and "Stop" buttons you would just change values of variable "isTimerRunning". Action for button "Reset" should be set variables "sec", "min", "hr" to zero and if you would like to stop timer at the same time set "isTimerRunning" to no.

Another thing that you should test in your application is accuracy, doing it like this (setting interval to 0.01 sec, and then updating counter) might be inaccurate.

zabbas
what is the mean by the (isTimerRunning) it is veriable or some thinking other
It is a variable, just like "counter", "sec", "min", "hr". It purpose is to control if timer is running.
zabbas
what is the mean by the (isTimerRunning) it is veriable or some thinking other
A: 

To make a variable go to 0 do the following:

variable = 0;

To make three variables go to 0

counter = 0;
sec = 0;
min = 0;

Simply put that in the action for your reset button.

JeremyP
it work but I want some more
@harishdy007: more what?
JeremyP