我制作了一个带有计时器的 RunLoop,它可以更新显示倒计时的标签。一旦倒计时达到零,我需要 RunLoop 停止,对于计时器正常结束的情况,我可以使用 runUntilDate,日期是当前日期 + 倒计时时间。问题是当用户在完成之前取消按钮的倒计时。我不知道如何告诉 RunLoop 停止取消按钮操作。这是 RunLoop 的代码:
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[self methodSignatureForSelector:@selector(updateCountdownLabel:)]];
[invocation setTarget:self];
[invocation setSelector:@selector(updateCountdownLabel:)];
[[NSRunLoop mainRunLoop] addTimer:[NSTimer timerWithTimeInterval:1 invocation:invocation repeats:YES] forMode:NSRunLoopCommonModes];
该方法只是告诉标签在每个循环中减少 1。
我可以告诉取消按钮将标签更改为零,并让运行循环选择器检查值是否为零,但运行循环自己的选择器可以告诉它停止吗?
cancelPerformSelector:target:argument:
cancelPerformSelectorsWithTarget:
这些是我发现的最接近的,但它们似乎不能从 RunLoops 自己的选择器内部工作,或者至少我没有以任何方式尝试过它们。
基本上我需要让按钮告诉 RunLoop 停止,或者以某种方式从它自己的选择器中停止 RunLoop。
谢谢。