2013-01-09 3 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

보기에서 시도하십시오 .Disappear .. –

+0

예, 해당 항목 만 수정합니다. [NSObject cancelPreviousPerformRequestsWithTarget : 셀렉터 : @selector (someMethod) object : nil]; 또는 [NSObject cancelPreviousPerformRequestsWithTarget : self]; – Ann

+0

viewWillDisappear도 시도했지만 작동하지 않습니다 ... – Ann

답변

0
.H에서

, viewWillAppear하는 .m에서

BOOL checkMethodCall; 

, viewWillDisappear에서

checkMethodCall = NO; 

다음

checkMethodCall = YES; 

,

-(void)textCalling 
{ 
    if(checkMethodCall) 
    { 
    return; 
    } 
    // Your code 
} 
그래서 코드를 변경 한 후에는 코드를 반환하지 않은 상태에서 코드를 반환하면됩니다.
+0

이것은 그가하고있는 일이 왜 작동하지 않는지를 설명하지 못합니다 – newacct

+0

@newacct 여기서는보기를 변경 한 후에 메소드 호출을 피하려고합니다. 그것은 코드 번호에서 이해가됩니까? 왜 부정 투표? –

+0

'[NSObject cancelPreviousPerformRequestsWithTarget : ...'는'performSelector : withObject : afterDelay :'로 예약 된 보류중인 수행을 취소하는 방법입니다. 따라서 완전히 다른 무언가를 찾는 것보다 오히려 자신이하는 일을 일하는 것이나, 그렇지 않은 이유를 설명해야합니다. – newacct

2

당신 "시간 지연 후 또 다시 메소드를 호출"입니다 그것을 중지 [timer invalidate]를 사용

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

및 viewllDidDisappear에서 사용하는 더 나은 you`d.

"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

opoperation을 사용하여 아무 것도 취소하고 둘 이상의 opreation을 수행하려는 경우 아무 작업이나 시작할 수 있습니다 nsoperationqueue으로 ...

-1

이것을 시도 ..

[자기 클래스] cancelPreviousPerformRequestsWithTarget : 자기 선택기 : @ 선택기 (것으로 someMethod) 목적 : 무기 호];