我目前正在这样做:
timer = [NSTimer timerWithTimeInterval:timeUntilAction target:self selector:@selector(actionToPerform) userInfo:nil repeats:NO];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSRunLoopCommonModes];
但我希望能够使该计时器无效。调用 invalidate 似乎没有做任何事情,我不知道还能尝试什么,因为我无法从 NSRunLoop
从我可以看到的任何实例方法中删除计时器 NSTimer
或 NSRunLoop
。
我应该如何设置一个方法 (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/