2011-12-22 14 views
0

私は自分のプロジェクト用のタイマを1秒ずつ減らします。しかし、カウンターが2回目の作業を開始すると、2秒後に3回、3秒後には1秒減少するように何をする必要がありますか?各ページの負荷に応じてタイマの減分が変化します

-(void)viewDidAppear:(BOOL)animated { 

    count=15; //timer set as 15 seconds 
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeats:YES]; //for decrementing timer 
} 

- (void)updateCounter:(NSTimer *)theTimer { 
    count -= 1; 
    NSString *s = [[NSString alloc] initWithFormat:@"%d", count]; 
    if(count==0) // alert for time expiry 
    { 
     alert = [[UIAlertView alloc] initWithTitle:@"Time Out!!" message:@"Your time is expired." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
     [alert show]; 
     self.timer.hidden=YES; 
     [self dismissModalViewControllerAnimated:YES]; 
    } 

    else { 
     self.timer.text = s; 
    } 

    [s release]; 
} 
+0

私のコードは私のコードは である - (ボイド)viewDidAppear: アニメーション(BOOL){カウント= 15。 //タイマーを15秒に設定します。 [NSTimer scheduledTimerWithTimeInterval:1.0fターゲット:セルフセレクタ:@selector(updateCounter:)userInfo:nil repeats:YES]; //タイマをデクリメントする場合 } – priya

+0

- (void)updateCounter:(NSTimer *)theTimer { count - = 1; NSString * s = [[NSString alloc] initWithFormat:@ "%d"、count]; if(count == 0)//時間切れのアラート { alert = [[UIAlertView alloc] initWithTitle:@ "タイムアウト!!"メッセージ:@ "あなたの時間は切れています。"デリゲート:self cancelButtonTitle:@ "Ok" otherButtonTitles:nil]; [アラートショー]; self.timer.hidden = YES; [self dismissModalViewControllerAnimated:YES]; } else { self.timer.text = s;} [sリリース]; } – priya

+0

あなたのコードを質問に載せると、読みやすくなります。 –

答えて

1

あなたが投稿したことから、複数のタイマーを作成していて、それらのいずれかを停止していません。したがって、3回後には、毎秒3発のタイマーが発動します。タイマーヒットは、あなたがそれを無効にしたいゼロ最小、で

[theTimer invalidate]

をしかし、あなたはまた、そのよう(@propertyに)作成したタイマーを握っ検討する必要がありますカウンターが実際にゼロになる前にユーザーがこのビューを他の方法のままにしておくと、それを無効にして解放することができます。

希望に役立ちます。

+0

これは私を助けました。今すぐタイマーがうまくいきます。しかし、私はバックボタンを使用するとき、それは私がそれを停止しようとすると、同じ問題が繰り返される戻って行くと、カウンタが実行されている間です。私はこれのための解決策を得るでしょうか? – priya

+0

よく見ると、あなたのビューが表示されるたびに新しいタイマーを開始するので、ビューが消えてしまったときにそのタイマーを停止(無効にする)し、解除する必要があります(viewWillDisappear :) –

+0

このhttp: //stackoverflow.com/questions/8641849/timer-decrement-gets-varied-for-each-page-load-when-using-back-button – priya

0

EDIT
(私はあなたが上記の解答を受け入れ、@Firoze Lafeerからの回答のためにあなたの「その動作していない」コメントを削除しているように見えた知っていることがわかり、これを投稿しました。しかし、私はこれを残すだろうと同じようにここには何らかの形で)

以下のコードをテストとして実行しても問題は発生しません。無効にしなくても、ログには複数の出力があります。

アプリのテキストフィールドが何をしているのかを見て、何が起こっているのかを見るのではなく、私が以下のようにロギングを試してみると、何が起こっているのかがわかりますか?

-(IBAction)runTimer:(id)sender { // attached to a button 
    [self invalidateTimer]; 
    count=15; //timer set as 15 seconds 
    //for decrementing timer 
    countimer = [NSTimer scheduledTimerWithTimeInterval:1.0f 
               target:self 
               selector:@selector(updateCounter:) 
               userInfo:nil 
               repeats:YES]; 
} 

-(void)updateCounter:(NSTimer *)theTimer { 
    count -= 1; 
    NSLog (@"count [email protected]%d", count);   
    if(count==0) // alert for time expiry 
    { 
     [self invalidateTimer]; 
    } 
} 

-(void)invalidateTimer { 
    if ([countimer isValid]) { 
     [countimer invalidate]; 
     NSLog(@"countimer invalidated "); 
     countimer = nil; 
    } 
} 
関連する問題