NSTimer 在我第一次告诉它时停止,然后拒绝停止
我已经在我的 UIViewController 标头中声明了一个 NSTimer ,如下所示:
NSTimer *mainTimer;
并且我在实现中的 viewWillAppear 方法中启动它,所以:
mainTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateAll) userInfo:nil repeats:YES];
我有一个名为 showInfo
的用于翻转视图的按钮(标准实用程序应用程序模板),它具有命令:
[mainTimer invalidate];
当我的应用程序启动时,计时器启动并执行操作。当我第一次点击该按钮并且视图翻转时,计时器停止。当我完成flipSideViewController
并且主视图重新出现时,计时器再次启动。到目前为止,很好。问题是,随后按下按钮(因此调用 [mainTimer invalidate]
不会使计时器停止。它只是继续下去。我可以来回翻转我想要的一切,它会继续更新出于笑容,我连续调用了两个 invalidate
,但崩溃了:
从
flipSideViewController
返回后,我调用了 viewWillAppear。
我自己,这会创建计时器。运行循环还会自动调用 viewWillAppear
,因此只有在我访问过另一面之后才会创建第二个计时器,因此计时器(单个)。奇怪的是,当我第一次翻转时,我可以创建两个同名的计时器......
I have declared an NSTimer
in my UIViewController
header as such:
NSTimer *mainTimer;
And I start it up in the viewWillAppear
method in the implementation, so:
mainTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateAll) userInfo:nil repeats:YES];
I have a button that flips the view over (standard utility app template) called showInfo
and it has the command:
[mainTimer invalidate];
When my app launches, the timer starts and does it's thing. The first time I tap that button and the view flips, the timer stops. When I'm done with the flipSideViewController
and the main view reappears, the timer starts again. So far, good. The problem is that subsequent button presses (and thus calls to [mainTimer invalidate]
don't make the timer stop. It just keeps going. I can flip back and forth all I want and it continues to update. For grins, I put two invalidate
calls in a row, but that crashes.
UPDATE:
Found the problem. Upon returning from the flipSideViewController
, I was calling viewWillAppear
myself, which would create the timer. The run loop also calls viewWillAppear
automatically, so a second timer was created. This only happened after I had visited the flip side once so the timer (single instance) would stop normally when I flipped the first time. Odd that I can create two timers with the same name though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一旦定时器失效,它就不会再触发。
因此,如果“计时器继续运行”,那么它是一个不同的计时器。确保你所讲的是你认为的实例。我见过很多错误,开发人员分配/初始化一个实例,然后加载 NIB 并导致特定类的不同实例发挥作用。我可以看到类似的事情在这里发生。
请注意,不可能在作为计时器目标的对象的释放中使计时器无效。计时器将保留目标,因此,在计时器第一次失效期间无法[正确]释放目标。
Once a timer is invalidated, it will never fire again.
Thus, if the "timer keeps going", then it is a different timer. Make sure that you are speaking to the instances you think you are. I've seen quite a few bugs where developer alloc/init an instance and then load a NIB and cause a different instance of a particular class to come into play. I could see something similar happening here.
Note that it is impossible to invalidate a timer in the dealloc of the object that is the target of the timer. The timer will retain the target and, thus, the target cannot be [correctly] deallocated within the timer first being invalidated.
我只是做了完全相同的事情,而且它似乎对我有用。我在 xcode 模板中做了什么:
转到 MainViewController.h 并输入:
转到 MainViewController.m 并添加:
就这样,它对我来说运行良好。这有什么帮助吗?也许您的问题出在其他地方 - 您在
updateAll
中做什么?I just did exactly the same, and it seems to work for me. What I did in the xcode template:
Go to MainViewController.h and type:
Go to MainViewController.m and add:
Just that, and it runs well for me. Does this help somehow? Maybe your problem is somewhere else - what do you do in
updateAll
?来自
[NSTimer invalidate]
的文档:所以我会调用 invalidate 然后释放你的计时器。看起来您一开始就没有保留计时器。因此,在创建时保留计时器,在需要时使其无效,然后在无效后释放。
下一个视图将出现,请确保您正在创建并保留新的计时器。
From the docs for
[NSTimer invalidate]
:So I would call invalidate and then release your timer. It doesn't look like you're retaining on the timer in the first place. So, retain the timer when you make it, invalidate it when you want to, then release after the invalidate.
Next viewWillAppear, make sure you're making and retaining on a new timer.
viewWillAppear 可能被调用多次。
您应该确保在创建计时器之前使其无效。
当然,如果您也在其他地方使计时器无效,则应该将其分配为 null。
It is possible viewWillAppear is called several times.
You should make sure the timer is invalidated before creating it
Of course, if you are also invalidating the timer on some other place, you should assign it to null.