iPhone如何取消停止性能选择器


74

在我的iPhone应用程序中,我使用以下功能在延迟一段时间后执行某些操作

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

有什么方法可以取消此performSelector并在延迟后停止执行某些操作?

Answers:


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

1
是否有任何快速选择?
morroko '16

2

我认为对人们来说一些实际的代码可能很有用,因此当我转到另一个屏幕时,有两个代码可以用来停止声音和滑动。

- (void)handleSwipeNext {

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

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

// do some swipe handling stuff
}

2

Swift 4.0版本:

NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(yourFunc), object: nil)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.