0
votes

I'm having some problems with my iPhone app that has three timers, two of which are initialized based on the main timer. Timer1 and Timer2 are suppose to change a label based on what the time is on the main timer. Right now the program moves very fast through my warmup array(not every 30 seconds like it should).

- (void)viewDidLoad
{    
     //these arrays hold strings 
    warmup = [[NSMutableArray alloc] initWithObjects:c1,c2,c3,c4,c5,c6, nil];

    legs = [[NSMutableArray alloc] initWithObjects:l,l2,l3,l4,l5,l6,l,l2,l3,l4,l5,l6,nil];

    //main timer for the program 
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)timerFired
{
    if((currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        UIProgressView *progressView = [[UIProgressView alloc] init];
        progressView.frame = CGRectMake(10,350,200,500);

        [self.view addSubview:progressView];

        if(currSeconds==0)
        {
            currMinute-=1;
            currSeconds=59;
        }
        else if(currSeconds>0)
        {
            currSeconds-=1;
        }
        if(currMinute>-1)
            [progress setText:[NSString stringWithFormat:@"%@%d%@%02d",@"Time : ",currMinute,@":",currSeconds]];

        [progressView setProgress:currMinute animated:YES];
    }
    else
    { 
        [timer invalidate];
    }

    if (currMinute>=27) 
    {
        title.text = @"warm-up";
        timer1 = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(rotatewarmup)userInfo:nil repeats:YES];
    }
    else if (currMinute<27) 
    {
        [timer1 invalidate];
        title.text = @"leg workout";
        timer2=[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(rotatelegs)userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer: timer2 forMode: NSDefaultRunLoopMode];
    }
}

-(void)rotatewarmup
{
    counter++ ;
    label45.text = [NSString stringWithFormat:@"workout: %@ ", [warmup objectAtIndex:counter]];
}

//the other function does the same but with the other array. 
1
can you please tell us your exact requirement? what are you trying to achieve ? - GoGreen
Timer1 and Timer2 are suppose to change a label based on the minute count of the main timer. So if the current minute is greater than 27, Timer1's function will be running. If it is less than 27 then Timer2 will be running. Hope that's clearer. - user3219182
Try it as mentioned by Tchami. - GoGreen

1 Answers

1
votes

Your problem lies in the fact that you are initialising timer1 once every second if currMinute >=27. And with each initialisation, your counter increments. Which is why your program is running through the warmup array so quickly. Perhaps try this:

if (currMinute>=27) 
    {
        title.text = @"warm-up";
        if(!timer1)
            timer1 = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(rotatewarmup)userInfo:nil repeats:YES];
    }
else{
        [timer1 invalidate];
        title.text = @"leg workout";
        if(!timer2){            
            timer2=[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(rotatelegs)userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer: timer2 forMode: NSDefaultRunLoopMode];
        }
    }

Same with timer2. How this works: the code checks first if the timer has been previously initialised, and if so, does not re-initialise it.