ios - 在 NSRunLoop 中取消 NSTimer

标签 ios nstimer nsrunloop

我目前正在这样做:

timer = [NSTimer timerWithTimeInterval:timeUntilAction target:self selector:@selector(actionToPerform) userInfo:nil repeats:NO];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSRunLoopCommonModes];

但我希望能够使该计时器无效。调用 invalidate 似乎没有做任何事情,我不知道还能尝试什么,因为我无法从 NSRunLoop 从我可以看到的任何实例方法中删除计时器 NSTimerNSRunLoop

我应该如何设置一个方法 (actionToPerform) 在一定的延迟 (timeUntilAction) 后触发,但在给定特定用户交互的情况下仍然能够取消操作。用户还应该能够在初始计时器(已取消的计时器)计划关闭之前重新启动计时器。

任何建议都会很棒!

谢谢

最佳答案

[self performSelector:@selector(actionToPerform) withObject:nil afterDelay:timeUntilAction]

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(actionToPerform) object:nil]

关于ios - 在 NSRunLoop 中取消 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927126/

相关文章:

iphone - iOS自定义UITableView中的滚动条

ios - 从 firebase 数据库中删除特定值

swift - 在 block 中调用时 NSTimer 不触发

iphone - 当应用程序在单独的线程中在 iOS 中处于事件状态时,从服务器定期轮询数据的最佳方法是什么?

objective-c - 操作系统 X : how to watch socket read events with NSRunLoop?

ios - Xcode 4 键盘快捷键切换构建目标目的地

iphone - 在两个 UITableView 之间转换时如何模仿 UINavigation 动画行为

iphone - 使用 NStimer 设置默认图像的图像动画

ios - 当主线程在 IOS 中被阻塞时,UIAlertView 在线程中关闭

objective-c - setNeedsLayout 如何工作?