我怎樣才能改變這種代碼,以便它有HH:MM:SS(小時,分,秒,對HH:MM:SS使用NSTimer?
,你可以告訴我,如果我需要在.H或.M,所以我知道添加代碼,其中一個
此刻它會像1,2,3,4等
嗨,大家好,只想讓你知道我是一個業餘的誘餌,你會複製過去,所以我知道你的意思,感謝
類別關注
保羅
.H
@interface FirstViewController : UIViewController {
IBOutlet UILabel *time;
NSTimer *myticker;
//declare baseDate
NSDate* baseDate;
}
-(IBAction)stop;
-(IBAction)reset;
@end
.M
#import "FirstViewController.h"
@implementation FirstViewController
-(IBAction)start {
[myticker invalidate];
baseDate = [NSDate date];
myticker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)stop;{
[myticker invalidate];
myticker = nil;
}
-(IBAction)reset;{
time.text = @"00:00:00";
}
-(void)showActivity {
NSTimeInterval interval = [baseDate timeIntervalSinceNow];
NSUInteger seconds = ABS((int)interval);
NSUInteger minutes = seconds/60;
NSUInteger hours = minutes/60;
time.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes%60, seconds%60];
}
如果你想測量時間,我建議你使用NSDate類而不是增加一些任意的整數變量。 – lawicko 2012-02-13 10:45:04
[NSTimer - Stopwatch]可能的重複(http://stackoverflow.com/questions/3180899/nstimer-stopwatch) – Caleb 2012-02-13 11:19:34
我在哪裏把NSdate?我是否替換了所有的NSTimer? – 2012-02-13 12:40:14