【问题标题】:iPhone - cancelPerformSelectorWithTarget not workingiPhone - cancelPerformSelectorWithTarget 不起作用
【发布时间】:2010-10-19 17:53:13
【问题描述】:

如果我在 UIView 的子类中这样做:

[self performSelector:@selector(doSomething) withObject:nil afterDelay:5];

然后像这样取消它(两个版本我都试过了):

 [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self];
 //[[NSRunLoop mainRunLoop] cancelPerformSelectorsWithTarget:self];

“doSomething”方法仍然被调用。我做错了什么?

【问题讨论】:

  • +1 我想我最近也注意到了这一点;我无法让它工作,所以我最终使用了不重复的NSTimer。 :P
  • 但这不一定与 NSTimer 相关。它只是取消一个请求......
  • 你确定这两种方法在同一个线程上运行吗?因为如果他们不这样做,那么他们会针对不同的运行循环。
  • @sol 我知道;我是说我的cancelPerformSelector 也无法正常工作,所以我使用了非重复计时器,我可以在需要时使用invalidate
  • @Dave - 我明白了。不是一个糟糕的解决方法。可能必须自己做。

标签: iphone ipad selector request-cancelling


【解决方案1】:

在 NSObject 类参考中:

cancelPreviousPerformRequestsWithTarget:selector:object:

取消之前的执行请求 注册于 performSelector:withObject:afterDelay:.

用途:

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2012-02-19
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多