我有一個NSDate變量。從這個變量中,我如何顯示在視圖中不斷更新的秒錶?我有一個NSDate,我如何顯示自動更新顯示計時器的秒錶?
我試過[NSTimer scheduledTimerWithTimeInterval]
,但不能得到它的工作,但也懷疑這是做到這一點的理想方式。
我有一個NSDate變量。從這個變量中,我如何顯示在視圖中不斷更新的秒錶?我有一個NSDate,我如何顯示自動更新顯示計時器的秒錶?
我試過[NSTimer scheduledTimerWithTimeInterval]
,但不能得到它的工作,但也懷疑這是做到這一點的理想方式。
嘗試這
- (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。
什麼是與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.
}
我已經試過了,其工作
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];
-(void)timerCallback{
NSDate *date=[NSDate date];
NSLog(@"%@",date);
}
更新一個時間間隔參數,你想要什麼,並在視圖中顯示它(我剛纔打印日誌);
我得到這個日誌:
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
在這裏你去哥們http://www.youtube.com/watch?v=5jmTQi98vec –
這個問題是每兩個月詢問至少一次。嘗試找到以前的版本之一。 –
[Stopwatch - NSTimer或NSDate]可能的重複(http://stackoverflow.com/questions/11375304/stopwatch-nstimer-or-nsdate) –