Question

i cant seem to get my NSTimer to stop when it reaches 0 i've looked around for an answer and i tryed everything please help.

-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:@"%i", mainint];
}

-(IBAction)start:(id)sender {
play.hidden = YES;
mainint = 60;
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(count) userInfo:nil repeats:YES];
if (timer1 == 0) {
    [timer1 invalidate];
    timer1 = nil;
}
}
Était-ce utile?

La solution

put your if condition in count() function

-(void)count {
    mainint -= 1;
    seconds.text = [NSString stringWithFormat:@"%i", mainint];
    if (mainint == 0) {
        [timer1 invalidate];
    }
}

Autres conseils

The issue is that your timer will never be nil (0) unless you set it yourself to nil. I think what you're trying to achieve is something like this :

-(void)count {
    mainint -= 1;
    seconds.text = [NSString stringWithFormat:@"%i", mainint];

    if (mainint <= 0) {
        [timer1 invalidate];
        timer1 = nil;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top