iPhone how to cancel stop performSelector

Iphone

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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
}

Solution 3 - Iphone

Swift 4.0 version :

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSatyamView Question on Stackoverflow
Solution 1 - IphonebeefonView Answer on Stackoverflow
Solution 2 - IphoneJScarryView Answer on Stackoverflow
Solution 3 - IphoneiOS.LoverView Answer on Stackoverflow