1

我在用

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

在一段时间延迟后一次又一次地调用一个方法。它工作正常,但我的问题是,即使我导航到下一堂课,执行选择器也在上一堂课中运行后台。

所以我尝试使用以下方法取消它

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

但它不起作用。我在viewDidDisappear. 谁能告诉我如何取消执行选择器?

这是我的代码

-(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];


}
4

4 回答 4

2

你“在一段时间延迟后一次又一次地调用一个方法”你最好使用

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

并在 viewllDidDisappear 中,用于[timer invalidate]停止它。

你的“某种方法”是这样写的吗?

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

也许您应该添加一个测试按钮,它会[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(someMethod) object:nil];在单击时调用。检查它是否有效。

您可以编写代码来解决问题。

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

在 viewDidDisappear 中设置 needStop = YES;

于 2013-01-09T06:20:06.840 回答
0

在.h中,

BOOL checkMethodCall;

在 .m viewWillAppear 中,

checkMethodCall = NO;

在视图中将消失,

checkMethodCall = YES;

然后,

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

因此,在更改视图后,方法中返回后的代码将不会执行..

于 2013-01-09T06:28:13.760 回答
-1

您只需使用 NSoperation 添加您的操作,如果您要执行多个操作,您可以在其中取消任何操作并启动任何操作,然后将其添加到 nsoperationqueue...

于 2013-01-09T06:57:28.373 回答
-1

尝试这个..

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

于 2013-01-09T07:02:39.173 回答