问题描述:

I have a popup in a custom view controller that is presented after 1 minute thanks to my NSTimer.

My NSTimer code:

[NSTimer scheduledTimerWithTimeInterval:60.0f

target:self selector:@selector(methodB:) userInfo:nil repeats:YES];`

The method that reveals the popup

- (void) methodB:(NSTimer *)timer

{

//Do calculations.

[self showPopupWithStyle:CNPPopupStyleFullscreen];

}

I'm trying to set the NSTimer to stop running if the [self showPopupWithStyle:CNPPopupStyleFullscreen]; is currently open, running or active.

Then I would like to start the NSTimer back up again if the popup view controller is NOT open, running or active.

Any help, samples or examples would be greatly appreciated!

My project is written in Objective-C.

EDIT:

I tried the answers in the suggested "possibly similar" answer and it doesn't seem to work for what I am doing. How do I stop NSTimer?

网友答案:

This seems to do the trick.

@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 you want to restart the timer just add this code where you'd like the action to start back up.

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

Hope this helps! :)

相关阅读:
Top