NSTimer 不调用方法

发布于 2024-11-08 21:05:36 字数 868 浏览 0 评论 0原文

我现在真的很沮丧,用谷歌搜索了整个互联网,偶然发现了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朮生 2024-11-15 21:05:36

timerWithTimeInterval 只是创建一个计时器,但不会将其添加到任何运行循环中执行。尝试

self.theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds 
                        target:self 
                      selector:@selector(showAlarm:)
                      userInfo:alarm repeats:NO];

一下。

timerWithTimeInterval simply creates a timer, but doesn't add it to any run loops for execution. Try

self.theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds 
                        target:self 
                      selector:@selector(showAlarm:)
                      userInfo:alarm repeats:NO];

instead.

萌面超妹 2024-11-15 21:05:36

另外不要忘记检查是否

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds 
                                     target:(id)target 
                                   selector:(SEL)aSelector 
                                   userInfo:(id)userInfo 
                                    repeats:(BOOL)repeats 

在主线程中调用。

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.

緦唸λ蓇 2024-11-15 21:05:36

您已经创建了一个 NSTimer 对象,但尚未安排它运行。 timerWithTimeInterval:target:selector:userInfo:repeats:创建一个可以安排稍后运行的计时器,例如,在应用程序启动时创建一个计时器,并在用户按下按钮时开始计时。 调用

[[NSRunLoop currentRunLoop] addTimer:theTimer forMode:NSDefaultRunLoopMode]

在 setTimerForAlarm 末尾

theTimer = [NSTimer timerWithTimeInterval:seconds 
                            target:self 
                          selector:@selector(showAlarm:)
                          userInfo:alarm repeats:NO];

或替换为

theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds 
                            target:self 
                          selector:@selector(showAlarm:)
                          userInfo:alarm repeats:NO];

创建计时器并立即安排它。

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

[[NSRunLoop currentRunLoop] addTimer:theTimer forMode:NSDefaultRunLoopMode]

at the end of setTimerForAlarm or replace

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.

弥繁 2024-11-15 21:05:36

好吧,您可能想实际安排您的 NSTimer 在运行循环上:)而不是 timerWithTimeInterval 使用scheduledTimerWithTimeInterval

theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds 
                        target:self 
                      selector:@selector(showAlarm:)
                      userInfo:alarm repeats:NO];

Well you may want to actually schedule your NSTimer on the run loop :) instead of timerWithTimeInterval use scheduledTimerWithTimeInterval.

theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds 
                        target:self 
                      selector:@selector(showAlarm:)
                      userInfo:alarm repeats:NO];
罗罗贝儿 2024-11-15 21:05:36

虽然所有答案都是正确的,但还有一个更简单的解决方案,根本不涉及 NSTimer。您的 setTimerForAlarm: 实现可以简化为一行简单的代码:

[self performSelector:@selector(showAlarm:) withObject:alarm afterDelay:[[alarm alarmDate] timeIntervalSinceNow]]

While all of the answers are right, there is an even simpler solution that doesn't involve a NSTimer at all. Your setTimerForAlarm: implementation can be reduced to one simple line:

[self performSelector:@selector(showAlarm:) withObject:alarm afterDelay:[[alarm alarmDate] timeIntervalSinceNow]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文