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];
}

解决方案 »

  1.   

    - (void)dealloc
    {
    [timer release];
    [_window release];
    [_viewController release];
    [super dealloc];
    }
    这里不需要release timer了
      

  2.   

    autorelease 的还用release 吗?
      

  3.   


    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    哪有autorelease?  还是说 onTimer内Autorelease了 timer就不用管了??
      

  4.   

    Apple的developer文档上说,只要不是用户自己用alloc等分配内存的对象,都是autorelease。
    volcan1987 说的没错~
      

  5.   

    scheduledTimerWithTimeInterval 生成的对象本身就是 autorelease
    - (void)dealloc
    {
    [timer release];
    }
    这里再释放一次,内存不异常才怪!虽然,异常不一定会引起程序崩溃⋯⋯
      

  6.   

    内存控制按照苹果的文档来就没问题了,感觉难的是api啊那么大那么多,什么时候才能学完更别说学明白了学无止境啊!
      

  7.   

    如果不知道一个对象的引用计数,可以随时打印出来:
    [obj retainCount]
      

  8.   


    是的,有的时候返回的是不正确的值,但是要知道引用计数还只能用这种方法。
    除非自己重写retainCount方法。
      

  9.   

    如何做一个输入法程序?
    苹果公司给了一例子,希望大家能帮忙。谢谢!
    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.