开发者

pedometer app in iphone

how to make time开发者_StackOverflowr 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];

}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜