当值<= 0时,无法让我的NSTimer结束

时间:2014-04-09 13:57:26

标签: ios nstimer

我似乎无法让我的NSTimer在达到0时停下来我已经四处寻找答案了,我尝试了所有的一切请帮助。

-(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;
}
}

2 个答案:

答案 0 :(得分:2)

将你的if条件放在count()函数

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

答案 1 :(得分:2)

问题是你的计时器永远不会是零(0),除非你自己设置为零。我认为你想要达到的目标是这样的:

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

    if (mainint <= 0) {
        [timer1 invalidate];
        timer1 = nil;
    }
}