如果 NSTimer 的重复属性等于 YES,我应该释放它吗?
有时我会对何时不释放对象感到困惑。我有:
NSTimer *timer2;
timer2 = [NSTimer scheduledTimerWithTimeInterval: (5)
target: self
selector: @selector(someMethod:)
userInfo: nil
repeats: YES];
每五秒执行一次的方法是:
-(void) someMethod:(NSTimer*)theTimer
{
NSLog(@"code got executed");
}
我有另一种方法,将另一个 nib 文件放置在我的根视图控制器上:
ViewControllerObjetivos *control = [ViewControllerObjetivos alloc];
[control initWithNibName:@"ViewControllerObjetivos" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc]
initWithRootViewController:control];
[self presentModalViewController:navControl animated:NO];
[navControl setNavigationBarHidden:YES];
[control release];
[navControl release];
当我调用最后一个方法时,一个新的 nib 文件 get 放置在根视图控制器上。并且 someMethod 仍然被调用!
所以我很困惑是否应该释放timer2,因为我没有使用init或alloc这个词,也没有使用copy来初始化它。那我该怎么办?我应该停止它然后调用我展示的最后一个方法吗?或者也许我应该发布整个 nib 文件,因为我将使用一个新文件?
Sometimes I get confused about when not releasing an object. I have:
NSTimer *timer2;
timer2 = [NSTimer scheduledTimerWithTimeInterval: (5)
target: self
selector: @selector(someMethod:)
userInfo: nil
repeats: YES];
and the method that get's executed every five seconds is:
-(void) someMethod:(NSTimer*)theTimer
{
NSLog(@"code got executed");
}
I have another method that places another nib file on my root view controller:
ViewControllerObjetivos *control = [ViewControllerObjetivos alloc];
[control initWithNibName:@"ViewControllerObjetivos" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc]
initWithRootViewController:control];
[self presentModalViewController:navControl animated:NO];
[navControl setNavigationBarHidden:YES];
[control release];
[navControl release];
when I call that last method a new nib file get's placed on the root view controller. And someMethod still gets called!
so I am confused if I should release the timer2 because I did not use the word init or alloc nor copy to initialize it. So what am I suppose to do? should I just stop it then call the last method that I showed? or maybe should I release the whole nib file since I am going to be working with a new one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
停止计时器的正确方法是调用其
无效
方法。在您的情况下,您可以将
timer2
存储在类的 ivar 中,然后在适当的时刻(即,当您显示第二个笔尖时)发送它invalidate
(如果我是没有错)。如您所见,不这样做将使计时器永远运行。如果您设置
重复
到NO
,计时器在第一次触发后将自动失效:The correct way to stop a timer is calling its
invalidate
method.In your case, you could store
timer2
in an ivar of your class and then send itinvalidate
at the proper moment, i.e., when you display the second nib (if I am not wrong). Not doing so will leave the timer running forever, as you witnessed.If you set
repeats
toNO
, the timer will be automatically invalidating after firing the first time:NSTimer的scheduledTimerWithTimeInterval返回一个它拥有的对象,所以你不需要保留它。如果以后需要使其失效(停止),则应自行保留。
NSTimer's scheduledTimerWithTimeInterval returns an object that it owns, so you do not need to retain it. If you need to invalidate it(stop it) later, you should retain it yourself.