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 に答える