如果某个动作是 运行,我如何设置 NSTimer 停止?
How do I set a NSTimer to stop if a certain action is running?
由于我的 NSTimer
.
,我在自定义视图控制器中有一个弹出窗口在 1 分钟后显示
我的NSTimer
代码:
[NSTimer scheduledTimerWithTimeInterval:60.0f
target:self selector:@selector(methodB:) userInfo:nil repeats:YES];`
显示弹窗的方法
- (void) methodB:(NSTimer *)timer
{
//Do calculations.
[self showPopupWithStyle:CNPPopupStyleFullscreen];
}
我正在尝试将 NSTimer
设置为在 [self showPopupWithStyle:CNPPopupStyleFullscreen];
当前打开、运行 或活动时停止 运行。
如果弹出视图控制器未打开,运行 或处于活动状态,我想再次启动 NSTimer
备份。
如有任何帮助、样本或示例,我们将不胜感激!
我的项目是用Objective-C写的。
编辑:
我尝试了建议的 "possibly similar" 答案中的答案,但它似乎不适用于我正在做的事情。如何停止 NSTimer
?
这似乎可以解决问题。
@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];
}
希望对您有所帮助! :)
由于我的 NSTimer
.
我的NSTimer
代码:
[NSTimer scheduledTimerWithTimeInterval:60.0f
target:self selector:@selector(methodB:) userInfo:nil repeats:YES];`
显示弹窗的方法
- (void) methodB:(NSTimer *)timer
{
//Do calculations.
[self showPopupWithStyle:CNPPopupStyleFullscreen];
}
我正在尝试将 NSTimer
设置为在 [self showPopupWithStyle:CNPPopupStyleFullscreen];
当前打开、运行 或活动时停止 运行。
如果弹出视图控制器未打开,运行 或处于活动状态,我想再次启动 NSTimer
备份。
如有任何帮助、样本或示例,我们将不胜感激!
我的项目是用Objective-C写的。
编辑:
我尝试了建议的 "possibly similar" 答案中的答案,但它似乎不适用于我正在做的事情。如何停止 NSTimer
?
这似乎可以解决问题。
@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];
}
希望对您有所帮助! :)