2016-07-18 3 views
1

NSTimer 덕분에 1 분 후에 표시되는 사용자 정의보기 컨트롤러에 팝업이 있습니다.특정 작업이 실행 중일 때 NSTimer를 중지하도록 설정하려면 어떻게합니까?

NSTimer 코드 :

[NSTimer scheduledTimerWithTimeInterval:60.0f 
           target:self selector:@selector(methodB:) userInfo:nil repeats:YES];` 

내가의 [self showPopupWithStyle:CNPPopupStyleFullscreen];가 현재 열려있는 경우 실행을 중지하기 위해 NSTimer을 설정하려고 실행 중이거나 활동하고있어 팝업에게

- (void) methodB:(NSTimer *)timer 
{ 
    //Do calculations. 
    [self showPopupWithStyle:CNPPopupStyleFullscreen]; 
} 

을 밝혀 방법.

그런 다음 팝업보기 컨트롤러가 실행 중이거나 실행 중이 아닌 경우 NSTimer을 다시 시작하고 싶습니다.

도움, 샘플 또는 예제를 제공해 주시면 대단히 감사하겠습니다.

내 프로젝트는 Objective-C로 작성되었습니다.

편집 :

내가 제안 "아마도 유사한"대답에서 답변을 시도하고 내가 뭐하는 거지 작동하지 않는 것 같습니다. NSTimer을 어떻게 중지합니까?

+0

[iPhone SDK의 가능한 복제본 : UIAlertView가 표시되는지 확인] (http://stackoverflow.com/questions/2528929/iphone-sdk-check-ifu-uialertview-is-showing) –

답변

1

이것은 트릭을 수행하는 것으로 보입니다.

@property (nonatomic, strong) CNPPopupController *popupController; 
- (void)_timerFired:(NSTimer *)timer; 

@end 

NSTimer *_timer; 

- (void)viewDidLoad { 
[super viewDidLoad]; 

if (!_timer) { 
    _timer = [NSTimer scheduledTimerWithTimeInterval:10.0f 
               target:self 
              selector:@selector(_timerFired:) 
              userInfo:nil 
              repeats:YES]; 
} 


} 

- (void)_timerFired:(NSTimer *)timer { 

if ([_timer isValid]) { 
    [self showPopupWithStyle:CNPPopupStyleFullscreen]; 
    [_timer invalidate]; 
} 
_timer = nil; 
    NSLog(@"ping"); 
} 

타이머를 다시 시작하려면 작업을 다시 시작하려는 곳에이 코드를 추가하십시오.

if (!_timer) { 
     _timer = [NSTimer scheduledTimerWithTimeInterval:10.0f 
                target:self 
               selector:@selector(_timerFired:) 
               userInfo:nil 
               repeats:YES]; 
    } 

희망이 있습니다. :)

+0

감사합니다. 당신! 이것은 효과가있다! –