我一直在尝试创建一个设置计时器的脚本,并在每次运行时将值减少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");
}
答案 0 :(得分: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");
}