将当前时间放入标签 [英] Putting current time into label
本文介绍了将当前时间放入标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
NSDate * StrDate = [NSDate date];
NSDateFormatter * Dateformat = [[NSDateFormatter alloc] init];
[Dateformat setDateFormat:@DD-MM-YYYY];
NSMutableString * DateStr = [Dateformat stringFromDate:StrDate];
[UserCntrl.timeDisplay setText:DateStr];
[Dateformat setDateFormat:@HH:MM];
NSMutableString * timeStr = [Dateformat stringFromDate:StrDate];
解决方案
在你的uiview中放置一个计划的定时器显示(或没有加载)方法:
[NSTimer scheduledTimerWithTimeInterval:1.0f // 1秒
target:self
选择器:@selector(updateTime :)
userInfo:nil
重复:YES];
然后,将此方法放在View Controller中:
$ b $ (void)updateTime:(id)sender
{
NSDate * StrDate = [NSDate date]; b
NSDateFormatter * Dateformat = [[NSDateFormatter alloc] init];
[Dateformat setDateFormat:@DD-MM-YYYY HH:mm:SS];
NSMutableString * DateStr = [Dateformat stringFromDate:StrDate];
[UserCntrl.timeDisplay setText:DateStr]; //或任何代码更新您的计时器。我没有检查这个bug。
}
这将一次调用updateTime:方法,更新控制器。
Ii am trying to get date and time using date but when i run application it takes first time executed application time and date in short time is not changed.
NSDate *StrDate = [NSDate date];
NSDateFormatter *Dateformat = [[NSDateFormatter alloc]init];
[Dateformat setDateFormat:@"DD-MM-YYYY"];
NSMutableString *DateStr = [Dateformat stringFromDate:StrDate];
[UserCntrl.timeDisplay setText:DateStr];
[Dateformat setDateFormat:@"HH:MM"];
NSMutableString *timeStr=[Dateformat stringFromDate:StrDate];
解决方案
Place a scheduled timer in your uiview did show (or did load) method:
[NSTimer scheduledTimerWithTimeInterval:1.0f // 1 second
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:YES];
Then, put this method in your View Controller:
- (void) updateTime:(id)sender
{
NSDate *StrDate = [NSDate date];
NSDateFormatter *Dateformat = [[NSDateFormatter alloc]init];
[Dateformat setDateFormat:@"DD-MM-YYYY HH:mm:SS"];
NSMutableString *DateStr = [Dateformat stringFromDate:StrDate];
[UserCntrl.timeDisplay setText:DateStr]; // or whatever code updates your timer. I didn't check this for bugs.
}
This will call the "updateTime:" method once a second, updating your controller.
这篇关于将当前时间放入标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文