2017-02-05 4 views
0

私はiOSデバイスで0から9までカウントしたいと思っていますが、私は常に9番しか見ることができません。私はタイマーを入れて5秒間表示しますが、動作しません。私は数字9を見ています。数字を順番に(0,1,2,3、..)見ることができますか?遅延を伴う数の変化を数えたり表示したりする方法は?

誰でもこの問題を手伝ってもらえますか?

- (IBAction)btnStart:(id)sender { 
    for(int i=0; i<10; i++) { 
     NSString* myNewString = [NSString stringWithFormat:@"%d", i]; 
     int64_t delayInSeconds = 5; 

     dispatch_time_t popTime = 
      dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 

     dispatch_after(popTime, dispatch_get_main_queue(), ^(void) { 
      _lbCounter.text =myNewString; 
     });   
    } 
} 
+1

'delayInSeconds'を' i * 5'に変更するだけです。 – rmaddy

答えて

0

あなたは10個のディスパッチを作成しています。目の瞬きの際に発生するメインキューでは、5秒後にすべてが起動します。

私は私の頭の外に、このヘッドを書き、それをコンパイルしませんでしたNSTimer

- (void)viewDidLoad { 
... 
// Fire `incrementLabel` every 5 seconds infinitely (repeats: YES) 
self.currentTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 
    target:self 
    selector:@selector(incrementLabel:) 
    userInfo:nil 
    repeats:YES]; 
... 
} 

- (void)incrementLabel { 
    self.currentCounter++; 
    if (self.currentCounter == 10) { 
    [self.currentTimer invalidate] 
    return; 
    } 

    _lbCounter.text = [NSString stringWithFormat:@"%ld", self.currentCounter]; 

} 

あなたは最高使用すると思いますが、それはこのように、多かれ少なかれになります。

関連する問題