0

我一直在尝试创建一个脚本,该脚本设置一个计时器并在每次运行时将一个值减少 33。理论上,计时器应该在 6 次运行后自行停止,但它会继续运行并达到负值。是否有一个原因?

-(void)checkValue:(NSTimer *)myTimer{

    if(pplint > 0){
        pplint = pplint-33;
        NSString *ppllefttext = [NSString stringWithFormat:@"%d", pplint];
        peopleleft.text = ppllefttext;
        NSLog(@"Reduced Timer");   
    }
    if(pplint < 0){
        NSLog(@"Stop Timer");
        [myTimer invalidate];
    }
}


- (IBAction)gotocongrats{
    pplint = 198;
    NSString *ppllefttext = [NSString stringWithFormat:@"%d", pplint];
    peopleleft.text = ppllefttext;
    NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                     target:self
                                                   selector:@selector(checkValue:)
                                                   userInfo:nil
                                                    repeats:YES];
    NSLog(@"Started Timer");
}
4

1 回答 1

0

我拿了你的代码并对其进行了一些编辑,它对我来说很好用。

-(void)checkValue:(NSTimer *)myTimer{

if(pplint > 0){
    pplint = pplint-33;

    NSLog(@"%i",pplint);
    NSLog(@"Reduced Timer");   
}
if(pplint <= 0){
    NSLog(@"Stop Timer");
    [myTimer invalidate];
    }
}

- (IBAction)gotocongrats{
pplint = 198;


NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                     target:self
                                                   selector:@selector(checkValue:)
                                                   userInfo:nil
                                                    repeats:YES];
NSLog(@"Started Timer");
}
于 2012-07-27T20:26:01.323 回答