我正在尝试使用日期获取日期和时间,但是当我运行应用程序时,它需要第一次执行应用程序时间,并且短时间内的日期不会改变。
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 did show (或 did load)方法中放置一个预定的计时器:
[NSTimer scheduledTimerWithTimeInterval:1.0f // 1 second
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:YES];
然后,将此方法放入您的视图控制器中:
- (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.
}
这将每秒调用一次“updateTime:”方法,更新您的控制器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)