Objective-C (iOS): 時計

ClockViewController

Objective-C: ClockViewController.h

#import <UIKit/UIKit.h>

@interface ClockViewController : UIViewController

@end

Objective-C: ClockViewController.m

#import "ClockViewController.h"

@interface ClockViewController ()

@end

@implementation ClockViewController

UILabel *label_;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(fire)
                                   userInfo:nil
                                    repeats:TRUE];
    label_ = [[UILabel alloc] init];
    [self.view addSubview:label_];
    [self fire];
}

- (void)fire
{
    NSDate *date = [NSDate date];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    [format setTimeZone:[NSTimeZone defaultTimeZone]];
    label_.text = [NSString stringWithFormat:@"%@", [format stringFromDate:date]];
    [label_ sizeToFit];
}

@end