ios - 为什么 `cancelPreviousRequestsWithTarget` 不取消 NSTimer?

标签 ios cocoa-touch cocoa nstimer performselector

我像这样安排一个 NSTimer 实例:

    [NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(someMethod) userInfo:nil repeats:YES];

我可以在我的 NSTimer 上调用 invalidate 来阻止它触发,但是

    [NSTimer cancelPreviousPerformRequestsWithTarget:self];

不停止计时器。我已经检查过,[NSRunLoop currentRunLoop] 完全相同(如预期的那样,这些是对用户点击的响应),当然 self 实例始终相同。为什么 cancel 不取消?

最佳答案

cancelPreviousPerformRequestsWithTarget: 是一个 NSObject 方法,不是 NSTimer 方法,它是 NSObject 的 performSelector: 方法的逆函数。

[NSTimer cancelPreviousPerformRequestsWithTarget:self][NSObject cancelPreviousPerformRequestsWithTarget:self]是一样的,它取消的是之前的任何[self performSelector:... withObject :... afterDelay:...] 消息。它与任何 NSTimer 实例无关。

将消息发送到 NSTimer 类而不是 NSObject 类的工作方式与将 alloc 发送到类的方式相同。没有类覆盖它;它之所以有效,是因为 NSObject 的实现是继承的,并且它出于同样的原因做同样的事情。

关于ios - 为什么 `cancelPreviousRequestsWithTarget` 不取消 NSTimer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7589644/

相关文章:

cocoa - 是否有等效的 -applicationDidReceiveMemoryWarning : on the Mac

objective-c - 使用 NSTableView 的最简单方法?

iOS:自动布局的问题。模拟器结果随着 Storyboard的变化而变化

ios - 画中画按钮已禁用

ios - CLLocationManager 即使在不需要时仍然存在

objective-c - UIImageView 中的 JPG 图像内存占用

python - Cocotron 与 pyobjc?

ios - 使用CloudKit共享时如何正确使用数据库订阅

iphone - 如何在iOS模拟器中测试UI Interpolating Motion Effect?

ios - 我的 subview 中的 UIButton 不起作用