【发布时间】:2011-12-24 17:35:16
【问题描述】:
我正在尝试了解 NSTimer,使用 Foundation 并打印到控制台。谁能告诉我我需要做什么才能使以下工作正常工作?它编译时没有错误,但没有激活我的startTimer 方法——没有打印。
我的目标是让一个方法调用另一个方法来运行一些语句,然后在设定的时间后停止。
#import <Foundation/Foundation.h>
@interface MyTime : NSObject {
NSTimer *timer;
}
- (void)startTimer;
@end
@implementation MyTime
- (void)dealloc {
[timer invalidate];
[super dealloc];
}
- (void)startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(runTimer:) userInfo:nil repeats:YES];
}
- (void)runTimer:(NSTimer *)aTimer {
NSLog(@"timer fired");
}
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MyTime *timerTest = [[MyTime alloc] init];
[timerTest startTimer];
[timerTest release];
[pool release];
return 0;
}
【问题讨论】:
-
请奖励过去帮助过您的人,并接受您之前问题的答案。
标签: objective-c macos nstimer foundation