2013-01-09 28 views
1

我使用如何取消上一个执行选择器?

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

时间延迟之后连连调用一个方法。它工作正常,但我的问题是,即使我导航到下一个类,执行选择器正在运行在前一课的背景。

所以我尝试使用以下

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

取消它,但它不工作。我在viewDidDisappear。任何人都可以告诉我如何取消执行选择器?

这里是我的代码

-(void)textCalling 
{ 


    NSString *[email protected]"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]; 


} 
+0

尝试在viewWillDisappear .. –

+0

是的,它的正确只有我们可以使用[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(someMethod)object:nil];或[NSObject cancelPreviousPerformRequestsWithTarget:self]; – Ann

+0

我尝试viewWillDisappear也但它不工作... – Ann

回答

0

在.H,

BOOL checkMethodCall; 

在.M viewWillAppear中,

checkMethodCall = NO; 

在viewWillDisappear,

checkMethodCall = YES; 

然后,

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

所以在改变视图后,代码返回后方法将不会例外..

+0

这并不能解释为什么他在做什么不起作用 – newacct

+0

@newacct在这里她只想在改变视图后避免方法调用。从代码中可以理解不是吗?为什么反对投票? –

+0

'[NSObject cancelPreviousPerformRequestsWithTarget:...''''''''''''''''''''''''''取消了一个挂在'performSelector:withObject:afterDelay:因此,与其提出完全不同的东西,你应该帮助OP做他们正在做的工作,或者解释为什么它没有。 – newacct

2

你“的时间延迟之后连连调用一个方法” you`d更好地利用

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

和viewllDidDisappear,使用[timer invalidate]停止它。

你的“somemethod”是这样写的吗?

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

+0

我已经编辑了这个问题请看看....所以如何使用定时器来使用多于一个执行选择器.. – Ann

+0

+1用于暗示'NSTimer' –

-1

您只需使用NSoperation添加您可以取消任何操作,并启动任何操作如果您想要执行多个操作然后添加它变成nsoperationqueue ...

-1

尝试此..

[[自类] cancelPreviousPerformRequestsWithTarget:自选择器:@selector(的someMethod)对象:无];