如何停止计时器触发的runloop?

nic*_*ico 9 iphone timer objective-c nsrunloop

如果我设置这样的runloop:

NSRunloop* loop = [NSRunloop currentRunLoop];
[runLoop addTimer:anyTimer forMode:NSDefaultRunLoopMode];
Run Code Online (Sandbox Code Playgroud)

我可以再次阻止它吗?或者是忽略我用来触发进一步行动的通知的唯一方法?

好的,我举一个问题的例子:

-(void)blinkeffekt:(double)pollingTime{

NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

if (pollingTime != 0) {
    NSTimeInterval interval =(double)pollingTime / 1000;
    NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(polling) userInfo:nil repeats:YES];
    [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
}
else {

    [timer invalidate];
}
Run Code Online (Sandbox Code Playgroud)

}

当然,这里有几个错误 - 毫无疑问.但我想,它显示了我的问题.直到现在,这个答案还无法解决.

我需要运行一个计时器并在以后停止它.理想情况下,在课堂上的另一个功能.但后来我不能访问"定时器"了,并runloop犯规让搞清楚,如果这样的"消息"是可用的.如果我为每个函数调用注册一个新的计时器,它将是非常无效的.

wil*_*ood 25

您需要向计时器发送一个invalidate消息,以将其从RunLoop中删除.

见文件

[anyTimer invalidate];
Run Code Online (Sandbox Code Playgroud)