gpt4 book ai didi

iphone - 显示计时器,计算日期、小时、分钟和秒数,然后实时递增

转载 作者:行者123 更新时间:2023-11-28 22:45:27 25 4
gpt4 key购买 nike

我正在尝试显示一个计时器,该计时器从一个日期计算出天数、小时数、分钟数和秒数,然后在 View /页面上实时递增。

什么是最好的方法?

最佳答案

你可以这样做:-

- (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);
}

关于iphone - 显示计时器,计算日期、小时、分钟和秒数,然后实时递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13317246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com