我在用
[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];
}