Objective c 如果某个操作正在运行,如何将NSTimer设置为停止?
由于我的Objective c 如果某个操作正在运行,如何将NSTimer设置为停止?,objective-c,nstimer,Objective C,Nstimer,由于我的NSTimer,我在自定义视图控制器中有一个弹出窗口,该弹出窗口在1分钟后显示 我的n计时器代码: [NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:@selector(methodB:) userInfo:nil repeats:YES];` 显示弹出窗口的方法 - (void) methodB:(NSTimer *)timer {
NSTimer
,我在自定义视图控制器中有一个弹出窗口,该弹出窗口在1分钟后显示
我的n计时器
代码:
[NSTimer scheduledTimerWithTimeInterval:60.0f
target:self selector:@selector(methodB:) userInfo:nil repeats:YES];`
显示弹出窗口的方法
- (void) methodB:(NSTimer *)timer
{
//Do calculations.
[self showPopupWithStyle:CNPPopupStyleFullscreen];
}
如果[self showPopupWithStyle:cnpopupStyleFullScreen]出现问题,我试图将NSTimer
设置为停止运行代码>当前处于打开、运行或活动状态
然后,如果弹出视图控制器未打开、未运行或未激活,我想再次启动NSTimer
备份
任何帮助,样品或例子将不胜感激
我的项目是用Objective-C编写的
编辑:
我尝试了建议的“可能相似”答案,但似乎对我正在做的事情不起作用。如何停止计时?这似乎起到了作用
@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];
}
希望这有帮助!:) 可能重复的