Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone how to cancel stop performSelector

Tags:

iphone

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?

like image 480
Satyam Avatar asked Sep 06 '10 05:09

Satyam


People also ask

How to cancel delay in swift?

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.

What is perform in Swift?

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.


2 Answers

[NSObject cancelPreviousPerformRequestsWithTarget:yourTarget selector:aSelector object: anArgument];
like image 164
beefon Avatar answered Oct 22 '22 04:10

beefon


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
}
like image 2
JScarry Avatar answered Oct 22 '22 05:10

JScarry