http://www.appsamuck.com/上的例子,针对XCODE4的报错修改一下。
原文在这里这是一个简单的小应用,练习练习Lable控件的使用,和Timer定时器的功能。先看下效果图。下载地址见原文
1、创建view-based Application. 命名为MyClock2、打开MyClockViewController.xib 调整背景色为黑色,拖入一个UILable控件3、按住control把Lablel连接到MyClockViewController.h,代码如下
#import@interface MyClockViewController : UIViewController {
UILabel *clockLable;
}@property (nonatomic, retain) IBOutlet UILabel *clockLable;
-(void) updateLable;
@end
4、MyClockViewController.m文件内设置字体和实现updateLable操作- (void)viewDidLoad
{
[clockLable setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:64.0]];
[super viewDidLoad];
}//更新lable为当前时间
-(void)updateLable{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
[clockLable setText:dateStr];
}
5、MyClockAppDelegate.h#import
@class MyClockViewController;
@interface MyClockAppDelegate : NSObject
{
NSTimer *timer;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MyClockViewController *viewController;
-(void) onTimer;
@end
6、MyClockAppDelegate.m设置计时器来执行updateLable- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
-(void) onTimer{
[self.viewController updateLable];
}- (void)applicationWillTerminate:(UIApplication *)application
{
[timer invalidate];
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}- (void)dealloc
{
[timer release];
[_window release];
[_viewController release];
[super dealloc];
}
原文在这里这是一个简单的小应用,练习练习Lable控件的使用,和Timer定时器的功能。先看下效果图。下载地址见原文
1、创建view-based Application. 命名为MyClock2、打开MyClockViewController.xib 调整背景色为黑色,拖入一个UILable控件3、按住control把Lablel连接到MyClockViewController.h,代码如下
#import@interface MyClockViewController : UIViewController {
UILabel *clockLable;
}@property (nonatomic, retain) IBOutlet UILabel *clockLable;
-(void) updateLable;
@end
4、MyClockViewController.m文件内设置字体和实现updateLable操作- (void)viewDidLoad
{
[clockLable setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:64.0]];
[super viewDidLoad];
}//更新lable为当前时间
-(void)updateLable{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
[clockLable setText:dateStr];
}
5、MyClockAppDelegate.h#import
@class MyClockViewController;
@interface MyClockAppDelegate : NSObject
{
NSTimer *timer;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MyClockViewController *viewController;
-(void) onTimer;
@end
6、MyClockAppDelegate.m设置计时器来执行updateLable- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
-(void) onTimer{
[self.viewController updateLable];
}- (void)applicationWillTerminate:(UIApplication *)application
{
[timer invalidate];
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}- (void)dealloc
{
[timer release];
[_window release];
[_viewController release];
[super dealloc];
}
{
[timer release];
[_window release];
[_viewController release];
[super dealloc];
}
这里不需要release timer了
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
哪有autorelease? 还是说 onTimer内Autorelease了 timer就不用管了??
volcan1987 说的没错~
- (void)dealloc
{
[timer release];
}
这里再释放一次,内存不异常才怪!虽然,异常不一定会引起程序崩溃⋯⋯
[obj retainCount]
是的,有的时候返回的是不正确的值,但是要知道引用计数还只能用这种方法。
除非自己重写retainCount方法。
苹果公司给了一例子,希望大家能帮忙。谢谢!
NumberInput_IMKit_Sample
Last Revision: Version 1.0, 2008-03-17
Illustrates an input method that uses the Mac OS X 10.5 InputMethodKit framework.