In my iPhone app, i'm using the following function to do something after some delay
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
Is there any way to cancel this performSelector and stop doing some thing after the delay?
To cancel that specific method call, you need to use the method cancelPreviousPerformRequests(withTarget:) on NSObject . Provide it with a target (where the method was going to be called), as well as the same selector and object you used when calling perform() , and it will cancel that delayed call.
Sends a specified message to the receiver and returns the result of the message. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+ Required.
[NSObject cancelPreviousPerformRequestsWithTarget:yourTarget selector:aSelector object: anArgument];
I thought it might be useful for people to see some actual code, so here are two that I use to stop sounds and swiping when I go to another screen.
- (void)handleSwipeNext {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSwipeNext) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(playPromptAndTarget) object:nil];
// do some swipe handling stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With