- (void)updateLabel { NSDate *dt = [NSDate date]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; // df.timeStyle = kCFDateFormatterMediumStyle; [df setDateFormat:@"HH:mm:ss"]; [label setText:[df stringFromDate:dt]]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window.backgroundColor = [UIColor clearColor]; label = [[UILabel alloc] initWithFrame: CGRectMake(0, 200, 320, 50)]; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont systemFontOfSize: 30]; label.textColor = [UIColor redColor]; label.textAlignment = UITextAlignmentCenter; timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(updateLabel) userInfo:nil repeats:YES]; [self.window addSubview: label]; [label release]; [self.window makeKeyAndVisible]; return YES; }效果图见相册.
收获:
1.NSTimer的使用,主要是scheduledTimerWithTimeInterval函数的使用.
2.NSDate/NSDateFormatter的使用
[formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
你的日期格式不对。
大写M用来表示月份,分钟是小写m;
大写S用来表示毫秒,秒是小写s;