-1

NSDate 変数があります。その変数から、ビューで継続的に更新されるストップウォッチを表示するにはどうすればよいですか?

私は試しまし[NSTimer scheduledTimerWithTimeInterval]たが、うまくいきませんが、それが理想的な方法であるかどうかも疑問です。

4

3 に答える 3

3

何が問題なのNSTimerですか?

- (void)startTimer {
    [NSTimer scheduledTimerWithTimeInterval:1/30.0f
                                     target:self
                                   selector:@selector(timerFired:)
                                   userInfo:[NSDate date]
                                    repeats:YES];
}

- (void)timerFired:(NSTimer *)timer {
    NSDate *startDate = timer.userInfo;
    NSTimeInterval secondsPassed = -[startDate timeIntervalSinceNow];
    // Update your label here.
}
于 2012-09-12T12:32:41.887 に答える
2

これを試してください

- (void)updateTimer
{
    static NSInteger counter = 0;
    [stopWatchLabel setText:[NSString stringWithFormat:@"Counter: %i", counter++]];
}

- (IBAction)onStartPressed:(id)sender {
    startDate = [[NSDate date]retain];

    // Create the stop watch timer that fires every 10 ms
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                      target:self
                                                    selector:@selector(updateTimer)
                                                    userInfo:nil
                                                     repeats:YES];
}

インターフェイス ビルダーで IBAction をボタンに接続し、stopWatchLabel を UILabel に接続する必要があります。

場所: http://www.apptite.be/tutorial_ios_stopwatch.php

于 2012-09-12T12:29:35.483 に答える
1

私はこれを試しましたが、うまくいきました

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

-(void)timerCallback{
    NSDate *date=[NSDate date];
    NSLog(@"%@",date);
}

timeInterval引数を必要なものに更新し、ビューに表示します(ログを印刷しました)。

このログを取得しました:

2012-09-12 18:04:11.252 S[19550:f803] 2012-09-12 12:34:11 +0000
2012-09-12 18:04:12.170 S[19550:f803] 2012-09-12 12:34:12 +0000
2012-09-12 18:04:13.170 S[19550:f803] 2012-09-12 12:34:13 +0000
2012-09-12 18:04:14.170 S[19550:f803] 2012-09-12 12:34:14 +0000
2012-09-12 18:04:15.170 S[19550:f803] 2012-09-12 12:34:15 +0000
2012-09-12 18:04:16.170 S[19550:f803] 2012-09-12 12:34:16 +0000
2012-09-12 18:04:17.170 S[19550:f803] 2012-09-12 12:34:17 +0000
2012-09-12 18:04:18.170 S[19550:f803] 2012-09-12 12:34:18 +0000
2012-09-12 18:04:19.171 S[19550:f803] 2012-09-12 12:34:19 +0000
2012-09-12 18:04:20.170 S[19550:f803] 2012-09-12 12:34:20 +0000
2012-09-12 18:04:21.170 S[19550:f803] 2012-09-12 12:34:21 +0000
于 2012-09-12T12:36:10.927 に答える