标签:
代码:
ViewController.h文件
@interface ViewController : UIViewController
{
UILabel *label;
NSDateFormatter *dateFormatter ;
}
ViewController.m文件
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addView];
}
#pragma -mark -functions
//初始化界面
-(void)addView
{
//时间
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
//label
label=[[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
label.text = [dateFormatter stringFromDate:[NSDate date]];
label.backgroundColor=[UIColor redColor];
[self.view addSubview:label];
//定时器
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
}
//更新时间
- (void)updateTime
{
label.text = [dateFormatter stringFromDate:[NSDate date]];
}
标签:
原文地址:http://www.cnblogs.com/yang-guang-girl/p/4607328.html