`
blessdyb
  • 浏览: 231779 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

在我们的iphone上如果有一个LED显示的电子时钟会有一种特别的感觉吧,呵呵。

 

首先,我们打开Xcode,点击FileNew Project,选择iPhone OSApplication,在这里我们选择View-based Application模版(我们的整个应用程序只有一个视图),点选Choose之后保存为LEDClick工程(默认整个工程会保存在/Users/当前登陆用户名/Documents下面)。之后点击OK就创建了了整个目录。

我们来看Groups&Files窗体,它分类显示了项目中的所有的信息。下面我们来进行具体的程序编写。对于我们来说,整个程序只有一个输出口(IBOutlet),我们会将当前的时候通过这个输出口显示出来。整个程序用到的主要有时间控制函数与计时器。

打开Classes文件夹中的LEDClockAppDelegate.h文件,这是一个应用程序委托的头文件,我们在其中添加一个NSTimer类的引用对象声明,同时添加一个无返回值的函数onInterval来实现时钟应用的计时功能,每隔一秒钟进行一次时钟计时.

//
//  LEDClockAppDelegate.h
//  LEDClock
//
//  Created by blessdyb on 09-9-5.
//  Copyright mobroad.com 2009. All rights reserved.
//

#import <UIKit/UIKit.h>

@class LEDClockViewController;

@interface LEDClockAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    LEDClockViewController *viewController;
	NSTimer *timer;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LEDClockViewController *viewController;

-(void) onInterval;

@end

 

 

 

之后进入委托程序的实现文件LEDClockAppDelegate.h中(如果你是在LEDClockAppDelegate.h中,那点击option+command+↑,就可以直接跳转到相应的实现文件中)。

//
//  LEDClockAppDelegate.m
//  LEDClock
//
//  Created by blessdyb on 09-9-5.
//  Copyright mobroad.com 2009. All rights reserved.
//

#import "LEDClockAppDelegate.h"
#import "LEDClockViewController.h"

@implementation LEDClockAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    timer=[NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onInterval) userInfo:nil repeats:YES];  
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

-(void) onInterval{
	[viewController interval];
}

- (void)dealloc {
	[timer release];
    [viewController release];
    [window release];
    [super dealloc];
}


@end

 

 

 

下面我们进行控制器类的编程实现。首先来看它的头文件,双击LEDClockViewController.h文件,我们在这里完成输入口的定义。由于我们的LED电子时钟是在一个标签上显示的,所以我们在这里声明一个UILabel的实例做为控制器类的属性,同时声明一个interval的无返回值方法(这下知道刚才委托类中的调用是怎么回事了吧)。

//
//  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,我们首先需要设置整个程序视图加载时时钟显示标签的字体,大小及初始化文本。这样,在计时器开始运行后,我们只需要每过一秒种改变显示标签的文本值就可以了。

 

//
//  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

 

 

接着我们需要完成程序的界面设计及元素连接,我们来看Resources目录下的文件,这里有三个文件,一个是LEDClockViewController.xib,一个是MainWindow.nib(主要是让应用程序委托、主窗口和视图控制器实例在运行时创建),还有一个LEDClock-info.plist(应用程序的各种参数配置)文件。

         双击LEDClockViewController.xib文件,之后会默认打开Interface Builder,我们会看到一个文件管理器窗口,其中有File’s Owner,First Responder View.双击View图标后会出现一个窗口,这就是我们应用程序在运行时最终被加载的视图,我们在菜单栏选取ToolsInspector,之后就会出现一个View Attributes的窗口,在里面可以编辑视图的各种属性,在这里我们让它的背景(Background)为黑色。之后再选取ToolsLibrary,打开库面板,我们在Library中找到Label控件,用鼠标选中后拖放到刚才打开的View窗口中,同样打开属性选择器后更改当前Label控件的大小及字体色彩,在这里我们设置它的色彩为红色。

         最后是整个程序最重要的一个步骤,元素的连接,我们选中xib窗口中的File’s Owner图标,同时按住Ctrl按键后往视图上的Label控件方向拖动,此时会出现一根蓝色的线条,当这根线条到达Label控件后Label控件变为选中状态,之后就会出现一个Outlets框,选中timerLabel后单击就可以完成了。(这个步骤完成了后可以先选中Label控件后点ToolsConnections Inspector后会看到弹出的窗口中的Referencing Outlets中出现一个连接项)。

    这样整个程序就编写完成。我们选择Xcode中的Run后打开iPhone模拟器就可以看到整个程序的运行结果了。

  • 大小: 139.9 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics