我怎样才能改变这种代码,以便它有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