2010-10-19 68 views
3

如果我的UIView的子类,这样做:iPhone cancelPerformSelectorWithTarget不工作

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

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

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

的 “DoSomething的”方法仍然被调用。我究竟做错了什么?

+1

+1我想我最近也注意到了这一点;我无法让它工作,所以我最终使用了一个不重复的'NSTimer'。 :P – 2010-10-19 17:56:34

+0

但这不一定与NSTimer有关。它只是取消一个请求... – sol 2010-10-19 18:03:06

+0

你确定两个方法在同一个线程上运行吗?因为如果他们不那么他们针对不同的运行循环。 – DarkDust 2010-10-19 18:06:34

回答

1

在NSObject类参考:

cancelPreviousPerformRequestsWithTarget:选择器:对象:

取消执行以前与 performSelector注册请求:withObject:afterDelay :.

用途:

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

希望这有助于。