NSTimer不会调用他的选择器

时间:2014-03-17 13:36:40

标签: ios objective-c nstimer

在我的应用中,我需要设置一个计时器,在这个计时器结束时,我会做一些动作。 我用以下代码创建了一个计时器:

@interface ViewController ()

@property(nonatomic)NSTimer *timer;

@end

然后我做了一些检查,如果我得到的音频是< 18000我应该启动计时器并等待10秒钟以显示警报。 我做了这个代码:

- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;

    if (frequencyRecived > 18000) {

        [self.timer invalidate];
        self.timer = nil;

        if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
            [self setTextInLabel:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
            [self setTextInLabel:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
            setTextInLabel:@"3"];
            water1 = water2 = water4 = NO;
            water3 = YES;
        }
        if (frequencyRecived >= 18450 && !water4) {
            [self setTextInLabel:@"4"];
            water1 = water2 = water3 = NO;
            water4 = YES;
        }
    } else {
        if (self.timer == nil) {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(noPosition) userInfo:nil repeats:NO];
        }
        water1 = water2 = water3 = water4 = NO;
    }
}

- (void)noPosition {
    [self.timer invalidate];
    self.timer = nil;
    [self setTextInLabel:@"Nessuna postazione"];
}

但是当我收到一个频率<1的音频信号时。 18000,我初始化NSTimer,但它不调用选择器方法,我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:中使用的方法签名必须具有NSTimer的参数,因为它将计时器传递给方法。如果你改变

,一切都应该正常
- (void)noPosition

- (void)noPosition:(NSTimer*)aTimer

并将其称为

self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(noPosition:) userInfo:nil repeats:NO];