WEB开发网
开发学院软件开发Java iphone学习之旅之实例:LED电子时钟 阅读

iphone学习之旅之实例:LED电子时钟

 2009-09-06 00:00:00 来源:WEB开发网   
核心提示: Objective-c代码 ////LEDClockViewController.h//LEDClock////Createdbyblessdybon09-9-5.//Copyrightmobroad.com2009.Allrightsreserved.//#import<UIKit/UI

Objective-c代码

// 
// LEDClockViewController.h 
// LEDClock 
// 
// Created by blessdyb on 09-9-5. 
// Copyright mobroad.com 2009. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
 
@interface LEDClockViewController : UIViewController { 
 IBOutlet 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 

上一页  1 2 3 4  下一页

Tags:iphone 学习 之旅

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接