2012-01-08 73 views
0

我有和NSTimer,當試圖格式化爲MM:SS時,計數器計數到一秒鐘,然後停止。這隻發生屁股很快,我除以60秒。我似乎無法看到爲什麼。有什麼建議麼?NSTimer時間格式表現奇怪

- (void)showActivity{ 
    int currentTime = [time.text intValue]; 
    int newTime = currentTime + 1; 
    int seconds = newTime % 60; 
    //int minutes = newTime/60; 
    time.text = [NSString stringWithFormat:@"%i", seconds]; 
} 
- (void)delayedMethod { 
    //Delay this action 
    [locationManager startUpdatingLocation]; 
    timeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showActivity) userInfo:nil repeats:YES]; 
} 

回答

0

%d格式字符串用於十進制值,並且您希望使用整數。這往往會導致怪異。嘗試使用:

time.text = [NSString stringWithFormat:@"%i:%i", minutes, seconds]; 
+0

試過,但沒有運氣... – arynhard 2012-01-08 19:30:21

+0

試圖用乘法代替除號和它的作品,但只要我又分爲,這是行不通的。奇怪。 – arynhard 2012-01-08 19:47:42

+0

你有沒有試過把一個NSLog語句,看看你實際得到的整數值是什麼?您似乎正在以兩種不同的方式使用time.text。最初你從它得到一個整數值,但是你將它重置爲MM:SS時間表示。如果您附上代碼以瞭解如何生成time.text,這將有所幫助。 – Suz 2012-01-09 16:55:58