iOS數字時鐘

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //定時器 反覆執行
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    
    
//設置停止按鈕
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];
    
    [button setTitle:@"STOP" forState:UIControlStateNormal];
    
    }
 
 
-(void)updateTime{
    UILabel *timeLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 300, 60)];
    
    timeLable.backgroundColor = [UIColor orangeColor];
    
    [self.view addSubview:timeLable];
    
    
    NSDate *currentDate = [NSDate date];
    
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init];
    
    [dataFormatter setDateFormat:@"YYYY - MM - dd   HH : mm : ss "];
    
    NSString *dateString = [dataFormatter stringFromDate:currentDate];
    NSLog(@"%@",dateString);
    
    timeLable.text = dateString;
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章