iPhone如何取消停止performSelector

在我的iPhone应用程序中,我使用下面的函数来做一些延迟之后的事情

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay; 

有没有办法取消这个performSelector,并在延迟之后停止做一些事情?

 [NSObject cancelPreviousPerformRequestsWithTarget:yourTarget selector:aSelector object: anArgument]; 

我认为可能有用的人看到一些实际的代码,所以这里有两个,我用来停止声音和刷卡时,当我去另一个屏幕。

 - (void)handleSwipeNext { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSwipeNext) object:nil]; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(playPromptAndTarget) object:nil]; // do some swipe handling stuff } 

Swift 4.0版本:

 NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(yourFunc), object: nil)