(iPhone)重复时我是否需要使计时器无效:不需要?
[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:NO];
当repeats:设置为NO时,我是否需要使指定选择器内的计时器无效?
谢谢
编辑
另一个问题,如果它自行失效,
如何正确取消这样的计时器?
因为使已经无效的计时器无效我认为会崩溃?
维护一个指向计时器的指针并将其设置为 nil 在将被触发的选择器中?
[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:NO];
When repeats: is set to NO, do I need to invalidate the timer inside the specified selector?
Thank you
Edit
Another question, if it self invalidates,
How do you properly cancel a such timer?
Since invalidating already-invalidated timer would crash I assume?
maintain a pointer to the timer and set it to nil inside the selector that will get fired?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,计时器会自行失效
No, the timer will invalidate itself
@Eugene,如果您在选择器方法中使用
then ,您需要提供一个像这样的函数
,这样当您想要使其无效时,您可以有一个像这样的条件
但是如果您在重复选项,则计时器将自行失效。
@Eugene if you are using
then in the selector method you need to give a function like this one
so when you want to invalidate it you can have a condition like this one
But if you are using
NO
in the repeat option then the timer will invalidate itself.您可以维护标志来保存计时器是否已被触发。
例如。
希望这有帮助。
You can maintain flag to save whether the timer has been fired or not.
eg.
Hope this helps.
如果repeats为YES,定时器将重复地重新调度自己,直到失效。如果为“否”,计时器将在触发后失效。
If repeats is YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
您缺少将计时器源添加到运行循环
addTimer:forMode:
you are missing to add the timer source to your runloop
addTimer:forMode: