NSTimer 不调用方法
我现在真的很沮丧,用谷歌搜索了整个互联网,偶然发现了SO,但仍然没有找到解决方案。
我正在尝试实现 NSTimer,但我定义的方法没有被调用。 (秒设置正确,用断点检查)。这是代码:
- (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]);
}
对象“theTimer”是用@property定义的:
@interface FooAppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate> {
@private
NSTimer *theTimer;
}
@property (nonatomic, retain) NSTimer *theTimer;
- (void) setTimerForAlarm:(Alarm *)alarm;
- (void) showAlarm:(Alarm *)alarm;
我做错了什么?
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. (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:
@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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
timerWithTimeInterval 只是创建一个计时器,但不会将其添加到任何运行循环中执行。尝试
一下。
timerWithTimeInterval simply creates a timer, but doesn't add it to any run loops for execution. Try
instead.
另外不要忘记检查是否
在主线程中调用。
Also don't forget to check if
is called in the main thread.
您已经创建了一个 NSTimer 对象,但尚未安排它运行。 timerWithTimeInterval:target:selector:userInfo:repeats:创建一个可以安排稍后运行的计时器,例如,在应用程序启动时创建一个计时器,并在用户按下按钮时开始计时。 调用
在 setTimerForAlarm 末尾
或替换为
创建计时器并立即安排它。
You've created an NSTimer object but you haven't scheduled it to be run. 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. Either call
at the end of setTimerForAlarm or replace
with
which creates a timer and immediately schedules it.
好吧,您可能想实际安排您的
NSTimer
在运行循环上:)而不是timerWithTimeInterval
使用scheduledTimerWithTimeInterval
。Well you may want to actually schedule your
NSTimer
on the run loop :) instead oftimerWithTimeInterval
usescheduledTimerWithTimeInterval
.虽然所有答案都是正确的,但还有一个更简单的解决方案,根本不涉及
NSTimer
。您的setTimerForAlarm:
实现可以简化为一行简单的代码:While all of the answers are right, there is an even simpler solution that doesn't involve a
NSTimer
at all. YoursetTimerForAlarm:
implementation can be reduced to one simple line: