【发布时间】:2011-05-20 05:24:54
【问题描述】:
假设我有一个附加了 IBAction 的按钮,当按下它时会触发几个动作,但必须以一秒的延迟触发一个特定的动作,并且只有当用户在此延迟中没有再次按下按钮时一秒钟。 代码如下所示:
@interface Image : UIView {
NSTimer *timer;
}
...other things...;
@end
@implementation Image
-(IBAction)startStopTimer{
...do something...;
...do something...;
[timer invalidate];
timer = [[NSTimer scheduledTimerWithTimeInterval:0.7
target:self
selector:@selector(delayedAction)
userInfo:nil
repeats:NO] retain];
}
-(void)delayedAction{
...do other things...;
}
@end
按原样,这段代码工作得很好:“delaiAvance”只有在用户不再按下按钮并等待至少一秒钟时才会触发。
最大的问题是:每次触发定时器,都会发生内存泄漏。
所以,问题是:我必须如何以及在哪里释放这个 NSTimer?
(dealloc方法中的[定时器释放]不起作用。)
【问题讨论】:
标签: iphone memory memory-leaks nstimer