显示计时器,从日期算起天,小时,分钟和秒,然后实时递增 [英] Display timer that works out Days, hours, minutes and seconds from a date then increments in real time
本文介绍了显示计时器,从日期算起天,小时,分钟和秒,然后实时递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试显示一个计时器,该计时器从日期算起几天,几小时,几分钟和几秒钟,然后在视图/页面上实时递增.
I am trying to Display a timer that works out Days, hours, minutes and seconds from a date then increments in real time on a view/page.
什么是最好的方法?
推荐答案
您可以这样操作:-
- (void)viewWillAppear:(BOOL)animated
{
[self start];
[super viewWillAppear:YES];
}
int currentTime;
- (IBAction)start{
currentTime = 0;
lbl=[[UILabel alloc]init];
//creates and fires timer every second
myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES]retain];
}
- (IBAction)stop{
[myTimer invalidate];
myTimer = nil;
}
- (IBAction)reset{
[myTimer invalidate];
lbl.text = @"00:00:00";
}
-(void)showTime{
currentTime++; //= [lbl.text intValue];
//int new = currentTime++;
int secs = currentTime % 60;
int mins = (currentTime / 60) % 60;
int hour = (currentTime / 3600);
int day = currentTime / (60 * 60 * 24);
lbl.text = [NSString stringWithFormat:@"%.2d:%.2d:%.2d:%.2d",day,hour, mins, secs];
NSLog(@"my lable == %@",lbl.text);
NSLog(@"my lable == %@",lbl.text);
}
这篇关于显示计时器,从日期算起天,小时,分钟和秒,然后实时递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文