Question

I am using

[self performSelector:@selector(someMethod) withObject:nil afterDelay:1];

to call a method again and again after a time delay. It's Working fine but my problem is, even though I navigate to next class, perform selector is running background in the previous class.

So I try to cancel it by using the following

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

but it's not working. I did this in viewDidDisappear. Can anyone tell me how to cancel perform selector?

Here is my code

-(void)textCalling
{


    NSString *str2=@"Something";
    UILabel *lbl =(UILabel*)[arrObj objectAtIndex:count2];
    [lbl setAlpha:0];
    lbl.numberOfLines=0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    lbl.text=str2;
    [UIView setAnimationDuration:0.0];
    [lbl setAlpha:0];
    [UIView commitAnimations];

}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{

    UILabel *lbl =(UILabel*)[arrObj objectAtIndex:count2];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [lbl setAlpha:1];
    [UIView commitAnimations];
    count2++;
    if (count2<[arrCourse count]) {
        [self performSelector:@selector(textCalling) withObject:nil afterDelay:1];
    }else{
        count2=0;

        [self performSelector:@selector(imageCallingCourse) withObject:nil afterDelay:1];
    }
}
-(void)imageCallingCourse
{

    imgViewObj.image=[UIImage imageNamed:@"Objective.png"];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStopImageCourse:finished:context:)];
    [UIView setAnimationDuration:0.0];
    [imgViewObj setAlpha:0];
    [UIView commitAnimations];
}

-(void)animationDidStopImageCourse:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    NSLog(@"animationDidStopImage1");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [imgViewObj setAlpha:1];
    [UIView commitAnimations];


}
Was it helpful?

Solution 2

In .h,

BOOL checkMethodCall;

In .m viewWillAppear,

checkMethodCall = NO;

In viewWillDisappear,

checkMethodCall = YES;

then,

-(void)textCalling
{
  if(checkMethodCall)
  {
    return;
  }
  // Your code
}

so after changing the view the code after return in method wiil not excecute..

OTHER TIPS

You "call a method again and again after a time delay" you`d better use

timer = [NSTimer scheduledTimerWithTimeInterval:1
                                               target:self
                                             selector:@selector(someMethod)
                                             userInfo:nil repeats:YES];

and in viewllDidDisappear , use [timer invalidate] to stop it.

your "somemethod " write like this?

- (void)somemethod
{
//your code
[self performSelector:@selector(someMethod) withObject:nil afterDelay:1];
}

maybe you should add a test button , it calls [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(someMethod) object:nil]; when it clicked. to check whether it works.

you can code like to solve the problem.

- (void)somemethod { 
    if (needStop) {
        return; 
    } //your code [self performSelector:@selector(someMethod) withObject:nil 
afterDelay:1]; 
}

in the viewDidDisappear set the needStop = YES;

You just use NSoperation to add your opreation in which you can cancel any and start any operation if more than one opreation you want to perform then add it into nsoperationqueue...

Try this..

[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(someMethod) object:nil];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top