2011-11-18 15 views
3

私のコードは次のとおりです。私のNSTimerのセレクタは動作しません。どうして?

-(void) timerRun{...} 

-(void) createTimer 
{ 
    NSTimer *timer; 
    timer = [NSTimer timerWithTimeInterval:1.0 
            target:self 
           selector:@selector(timerRun) 
           userInfo:nil 
            repeats:YES]; 
} 

viewDidLoad 
{ 
    [NSThread detachNewThreadSelector:@selector(createTimmer) 
          toTarget:self withObject:nil]; 
    ... 

} 

私はデバッグする、[OK]を実行しますが、この方法はtimerRunが実行されないcreateTimer方法?

+0

'@selector(createTimmer)'おそらく単なるタイプミスです:

コードは、おそらくのように見えるのだろうか? – Tommy

+0

なぜタイマーを作成するために別のスレッドを使用しているのか不思議です...何らかの理由で長時間動作することが予想されますか? – devios1

答えて

12

を追加することができますいずれか

ちょうどタイマーを作成する実行を開始しません。作成してスケジュールする必要があります。

バックグラウンドスレッドで実行したい場合は、実際にはそれよりも少し多くの作業を行う必要があります。 NSTimerは、NSRunloopに添付されています。これは、イベントループのCocoa形式です。それぞれNSThreadには本質的に実行ループがありますが、明示的に実行するように指示する必要があります。

タイマー付きの実行ループは無期限に実行できますが、自動解放プールを管理していないため、実行したくない可能性があります。

要するに、(i)タイマーを作成したいと思うかもしれません。 (ii)そのスレッドの実行ループにアタッチします。 (iii)自動解放プールを作成するループを入力し、実行ループを少し実行した後、自動解放プールを廃棄します。

// create timer 
timer = [NSTimer timerWithTimeInterval:1.0 
           target:self 
           selector:@selector(timerRun) 
           userInfo:nil 
           repeats:YES]; 

// attach the timer to this thread's run loop 
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 

// pump the run loop until someone tells us to stop 
while(!someQuitCondition) 
{ 
    // create a autorelease pool 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    // allow the run loop to run for, arbitrarily, 2 seconds 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]]; 

    // drain the pool 
    [pool drain]; 
} 

// clean up after the timer 
[timer invalidate]; 
+0

ありがとう、私は間違いを見つけた。私はあなたのコードを追加します: "while(!someQuitCondition) { //自動解放プールを作成する NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //実行ループを任意に2秒間実行できるようにする [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]]; //プールを排水する [プール排水]; } "の場合、timerRunはokを実行します。 – user797876

2

scheduledTimerWithTimeIntervalで使用するメソッドシグネチャ:target:selector:userInfo:repeats:NSTimerが引数として渡されるため、NSTimerの引数が必要です。あなたがにあなたのメッセージの署名を変更する必要があり

(void)timerRun:(NSTimer *)timer; 

あなたは、引数に何もする必要はありませんが、それはあるはずです。それが今の引数を受け入れるようものcreateTimerにセレクタは@selector(timerRun :)になります:

timer = [NSTimer timerWithTimeInterval:1.0 
           target:self 
          selector:@selector(timerRun:) 
          userInfo:nil 
          repeats:YES]; 
5

あなたはそれを実行するためにタイマーをスケジュールする必要があります。実行ループに接続され、必要に応じてタイマーが更新されます。あなたはcreateTimer

[NSTimer scheduledTimerWithTimeInterval:1.0 
            target:self 
           selector:@selector(timerRun) 
           userInfo:nil 
            repeats:YES]; 

を変更したり、

[[NSRunLoop currentRunLoop] addTimer:timer forModes:NSRunLoopCommonModes]; 
+0

iは変化が、私は、コードを移動する方法timerRunは依然として、runnigされていない "[NSTimer scheduledTimerWithTimeInterval:1.0 ターゲット:自己 セレクタ:@selector(timerRun) ユーザー情報: 反復ゼロ:YES];" viewDidLoadでは、timerRunはokを実行します。私は問題が別のスレッドだと思う。しかし、私はメインスレッドでタイマーを実行させたくない。 – user797876

+0

トミーの答えを見て、彼ははるかに詳細であなたの問題を修正した。 –

関連する問題