4
[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:NO];

repeats:がNOに設定されている場合、指定されたセレクター内のタイマーを無効にする必要がありますか?

ありがとうございました

編集

別の質問、それが自己無効化する場合、

そのようなタイマーをどのように適切にキャンセルしますか?
すでに無効になっているタイマーを無効にするとクラッシュするので、私は推測しますか?

タイマーへのポインターを維持し、起動されるセレクター内でそれをnilに設定しますか?

4

5 に答える 5

7

いいえ、タイマーはそれ自体を無効にします

于 2011-03-04T08:23:34.663 に答える
1

@Eugeneを使用している場合

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:YES];

次に、selectorメソッドで、このような関数を指定する必要があります

- (void)timerFireMethod:(NSTimer*)theTimer

したがって、それを無効にしたい場合は、このような状態にすることができます

if(workDone == YES)
{
   [theTimer invalidate];
}

ただしNO、繰り返しオプションで使用している場合、タイマーはそれ自体を無効にします。

于 2011-03-04T08:43:09.367 に答える
1

フラグを維持して、タイマーが起動したかどうかを保存できます。

例えば。

    BOOL gameOver = NO;
    NSTimer * gameOverTimer;


-(void)startGame
{

     gameOverTimer =    [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(stopLevel:) userInfo:nil repeats:NO]; 
     // your code
}

-(void)stopLevel:(id)sender
{
     gameOver = YES;
     // your code
}

-(void)levelFinishedSuccesfully
{
     // this method will get called if user finishes the level before your timer ends/stops the level. So the timer is valid and we need to invalidate it
     if(!gameOver)
     {
          [gameOverTimer invalidate];
          gameOverTimer = nil;
     }    
      // your code
}

お役に立てれば。

于 2011-03-04T08:47:04.707 に答える
0

repeatsがYESの場合、タイマーは無効になるまで繰り返しスケジュールを変更します。NOの場合、タイマーは起動後に無効になります。

于 2011-03-04T08:29:59.650 に答える
0

タイマーソースをrunloopに追加することができません

addTimer:forMode:

于 2014-04-03T22:59:04.233 に答える