0

我在使用 NSTimers 时遇到了很多麻烦。我以前用过它们,但这个计时器就是不想开火。

-(void) enqueueRecordingProcess
{
    NSLog(@"made it!");
    NSTimer *time2 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(warnForRecording) userInfo:nil repeats:YES];

}

-(void) warnForRecording
{
    NSLog(@"timer ticked!");
    if (trv > 0) {
        NSLog(@"Starting Recording in %i seconds.", trv);
    }
}

我不明白为什么这不会运行。我什至试过这个:

- (void)enqueueRecordingProcess
{
    NSLog(@"made it!");
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(warnForRecording) userInfo:nil repeats:YES];
}

 - (void)warnForRecording
{
    NSLog(@"timer ticked!");
}

怎么了?

4

3 回答 3

2

不确定这是否可以解决,但来自文档

The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
于 2012-06-03T23:38:33.950 回答
0

我不确定您的具体情况,但是您需要为 runloop 提供服务才能获取事件,包括计时器。

于 2012-06-04T04:12:19.673 回答
0
// Yes.  Here is sample code (tested on OS X 10.8.4, command-line).
// Using ARC:
// $ cc -o timer timer.m -fobjc-arc -framework Foundation
// $ ./timer
//

#include <Foundation/Foundation.h>

@interface MyClass : NSObject
@property NSTimer *timer;
-(id)init;
-(void)onTick:(NSTimer *)aTimer;
@end

@implementation MyClass
-(id)init {
    id newInstance = [super init];
    if (newInstance) {
        NSLog(@"Creating timer...");
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0
            target:self
            selector:@selector(onTick:)
            userInfo:nil
            repeats:YES];
    }
    return newInstance;
}

-(void)onTick:(NSTimer *)aTimer {
    NSLog(@"Tick");
}
@end

int main() {
    @autoreleasepool {
        MyClass *obj = [[MyClass alloc] init];
        [[NSRunLoop currentRunLoop] run];
    }
    return 0;
}
于 2013-06-13T04:00:14.967 回答