【发布时间】:2011-05-20 15:43:18
【问题描述】:
我现在真的很沮丧,用谷歌搜索了整个互联网,偶然发现了 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;
我做错了什么?
【问题讨论】:
-
showAlarm:(Alarm *)alarm的方法签名应该改为showAlarm:(NSTimer *)timer。然后你会得到带有Alarm *alarm = [timer userInfo]的Alarm对象。
标签: objective-c cocoa nstimer