How to kill a selector that is configured to fire after a delay (on iPhone)?

If I have a view with performSelector set after a delay:

[self performSelector:@selector(generateBall) withObject:NULL afterDelay:1.5]; 

... but I delete FromSuperview that the view before the selector fires (for example, due to user interaction), then my application crashes.

Is there a way to kill the delay selector in the dealloc method for this view?

EDIT:

I tried both:

 [[NSRunLoop mainRunLoop] cancelPerformSelector:theBall target:self argument:nil]; 

and

 [[NSRunLoop currentRunLoop] cancelPerformSelector:theBall target:self argument:nil]; 

and while both work (letting me load a new view), loading the previous view ends up giving me a gray screen.

I was not able to find any tutorials or other information on cancelPerformSelector other than the Apple documents that were indicated, and the documentation on threads and startup loops seems to be very confusing (mainly because they do not display the working code samples that would make it easier for me to go through and understand what is happening).

+6
iphone selector
source share
3 answers

Since I use the performSelector: afterDelay function, the only way I was able to “kill” any previously requested but not running functions is using:

 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:theBall object:nil]; 

The following code example shows how this works (create a new Xcode View template project called "select" and replace the selectViewController.h file with this):

 #import "selectViewController.h" @implementation selectViewController UILabel *lblNum; UIButton *btnStart, *btnStop; int x; - (void) incNum { x++; lblNum.text = [NSString stringWithFormat:@"%i", x]; [self performSelector:@selector(incNum) withObject:NULL afterDelay:1.0]; } - (void) stopCounter { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(incNum) object:NULL]; } - (void)viewDidLoad { x = 0; lblNum = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; lblNum.textAlignment = UITextAlignmentCenter; [self.view addSubview:lblNum]; btnStart = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnStart.frame = CGRectMake(40, 270, 240, 30); [btnStart setTitle:@"start" forState:UIControlStateNormal]; [btnStart addTarget:self action:@selector(incNum) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnStart]; btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnStop.frame = CGRectMake(40, 310, 240, 30); [btnStop setTitle:@"stop" forState:UIControlStateNormal]; [btnStop addTarget:self action:@selector(stopCounter) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnStop]; [self performSelector:@selector(incNum) withObject:NULL afterDelay:1.0]; [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidUnload { } - (void)dealloc { [lblNum release]; [super dealloc]; } @end 
+15
source share

I found this to work fine:

 [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
+3
source share

All Articles