let UILabel *timerLabel;
}
@property (nonatomic, retain) UILabel *timerLabel;
-(void) interval;
@end
下面进入控制器类的实现文件中对刚才的定义进行实现,双击LEDClockViewController.m,我们首先需要设置整个程序视图加载时时钟显示标签的字体,大小及初始化文本。这样,在计时器开始运行后,我们只需要每过一秒种改变显示标签的文本值就可以了。
Objective-c代码
//
// LEDClockViewController.m
// LEDClock
//
// Created by blessdyb on 09-9-5.
// Copyright mobroad.com 2009. All rights reserved.
//
#import "LEDClockViewController.h"
@implementation LEDClockViewController
@synthesize timerLabel;
-(void) interval{
NSUInteger unitFlags=NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date=[NSDate date];
NSDateComponents *now=[calendar components:unitFlags fromDate:date];
int hour=[now hour];
int minute=[now minute];
int second=[now second];
[timerLabel setText:[NSString stringWithFormat:@"%02d:%02d:%02d",hour,minute,second]];
[calendar release];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[timerLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:50.0]];
[timerLabel setText:@"电子时钟"];
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn''t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren''t in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[timerLabel release];
[super dealloc];
}
@end
iphone学习之旅之实例:LED电子时钟(3)
时间:2009-11-12 javaeye blessdyb
接着我们需要完成程序的界面设计及元素连接,我们来看Resources目录下的文件,这里有三个文件,一个是LEDClockViewController.xib,一个是MainWindow.nib(主要是让应用程序委托、主窗口和视图控制器实例在运行时创建),还有一个LEDClock-info.plist(应用程序的各种参数配置)文件。
双击LEDClockViewController.xib文件,之后会默认打开 |