如果 NSTimer 的重复属性等于 YES,我应该释放它吗?

发布于 2024-11-25 09:05:15 字数 1161 浏览 0 评论 0原文

有时我会对何时不释放对象感到困惑。我有:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福丶如此 2024-12-02 09:05:16

停止计时器的正确方法是调用其 无效方法。

无效

阻止接收器再次触发并请求将其从运行循环中删除。

在您的情况下,您可以将 timer2 存储在类的 ivar 中,然后在适当的时刻(即,当您显示第二个笔尖时)发送它 invalidate (如果我是没有错)。如您所见,不这样做将使计时器永远运行。

如果您设置 重复NO,计时器在第一次触发后将自动失效:

重复

如果是,计时器将重复重新安排自己,直到失效。如果为“否”,计时器将在触发后失效。

The correct way to stop a timer is calling its invalidate method.

invalidate

Stops the receiver from ever firing again and requests its removal from its run loop.

In your case, you could store timer2 in an ivar of your class and then send it invalidate 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 to NO, the timer will be automatically invalidating after firing the first time:

repeats

If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

稚气少女 2024-12-02 09:05:16

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文