[英]NSTimer doesn't call method
I'm really frustrated now, googled the whole internet, stumbled through SO and still didn't find a solution. 我现在真的很沮丧,谷歌搜索整个互联网,偶然发现,仍然没有找到解决方案。
I'm trying to implement an NSTimer, but the method which I defined doesn't get called. 我正在尝试实现NSTimer,但我定义的方法不会被调用。 (seconds are set correctly, checked it with breakpoints).
(正确设置秒数,使用断点检查)。 Here is the code:
这是代码:
- (void) setTimerForAlarm:(Alarm *)alarm {
NSTimeInterval seconds = [[alarm alarmDate] timeIntervalSinceNow];
theTimer = [NSTimer timerWithTimeInterval:seconds
target:self
selector:@selector(showAlarm:)
userInfo:alarm repeats:NO];
}
- (void) showAlarm:(Alarm *)alarm {
NSLog(@"Alarm: %@", [alarm alarmText]);
}
The object "theTimer" is deined with @property: 对象“theTimer”用@property定义:
@interface FooAppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate> {
@private
NSTimer *theTimer;
}
@property (nonatomic, retain) NSTimer *theTimer;
- (void) setTimerForAlarm:(Alarm *)alarm;
- (void) showAlarm:(Alarm *)alarm;
What am I doing wrong? 我究竟做错了什么?
timerWithTimeInterval simply creates a timer, but doesn't add it to any run loops for execution. timerWithTimeInterval只是创建一个计时器,但不会将它添加到任何运行循环中以便执行。 Try
尝试
self.theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:@selector(showAlarm:)
userInfo:alarm repeats:NO];
instead. 代替。
Also don't forget to check if 另外不要忘记检查是否
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats
is called in the main thread. 在主线程中调用。
You've created an NSTimer object but you haven't scheduled it to be run. 您已经创建了一个NSTimer对象,但尚未安排它运行。 timerWithTimeInterval:target:selector:userInfo:repeats: creates a timer that you can schedule to run later, for example, to create a timer at application launch and have it start counting when the user presses a button.
timerWithTimeInterval:target:selector:userInfo:repeats:创建一个计时器,您可以安排稍后运行,例如,在应用程序启动时创建计时器,并在用户按下按钮时开始计时。 Either call
要么打电话
[[NSRunLoop currentRunLoop] addTimer:theTimer forMode:NSDefaultRunLoopMode]
at the end of setTimerForAlarm or replace 在setTimerForAlarm结束或替换
theTimer = [NSTimer timerWithTimeInterval:seconds
target:self
selector:@selector(showAlarm:)
userInfo:alarm repeats:NO];
with 同
theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:@selector(showAlarm:)
userInfo:alarm repeats:NO];
which creates a timer and immediately schedules it. 它创建一个计时器并立即安排它。
Well you may want to actually schedule your NSTimer
on the run loop :) instead of timerWithTimeInterval
use scheduledTimerWithTimeInterval
. 那么你可能想在运行循环中实际安排你的
NSTimer
:)而不是timerWithTimeInterval
使用scheduledTimerWithTimeInterval
。
theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:@selector(showAlarm:)
userInfo:alarm repeats:NO];
While all of the answers are right, there is an even simpler solution that doesn't involve a NSTimer
at all. 虽然所有答案都是正确的,但有一个更简单的解决方案,根本不涉及
NSTimer
。 Your setTimerForAlarm:
implementation can be reduced to one simple line: 您的
setTimerForAlarm:
实现可以简化为一个简单的行:
[self performSelector:@selector(showAlarm:) withObject:alarm afterDelay:[[alarm alarmDate] timeIntervalSinceNow]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.