@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
NSTimer *timer;
UILabel *lblTimeDigit;
}
@property(retain, nonatomic) NSTimer *timer;
@property(retain, nonatomic) UILabel *lblTimeDigit;
@end
.m 파일.
@implementation MainViewController
@synthesize timer;
@synthesize lblTimeDigit;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
// 스크린의 크기 가져오기
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// UILabel 객체 초기화
lblTimeDigit = [[UILabel alloc] initWithFrame:CGRectMake(screenSize.width-220, 10, 200, 50)];
[self.view addSubview:lblTimeDigit]; // 라벨 배치시키기
[lblTimeDigit release]; // 메모리 해제
lblTimeDigit.backgroundColor = [UIColor clearColor]; // 라벨 뒷배경색 : 투명
lblTimeDigit.textColor = [UIColor whiteColor]; // 글씨 색 : 흰색
lblTimeDigit.textAlignment = UITextAlignmentRight; // 정렬 : 오른쪽
lblTimeDigit.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:70]; // 그림자
lblTimeDigit.shadowOffset = CGSizeMake(0, -1.0); // 그림자 위치 설정
[lblTimeDigit setFont:[UIFont fontWithName:@"ArialRoundedMTBold" size:24.0]]; // 폰트
self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; // 타이머 설정 (인터벌 : 1초, 반복하기)
}
- (void) updateTime // 타이머 함수
{
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *comp = [calendar components:unitFlags fromDate:date]; // 날짜, 시간 가져오기
int hourOfDay = [comp hour];
int minuteOfHour = [comp minute];
int secondOfMinute = [comp second];
self.lblTimeDigit.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hourOfDay,minuteOfHour,secondOfMinute];
}
- (void)dealloc {
[lblTimeDigit release];
[timer release];
[super dealloc];
}
@end