Question

In my app I need to setup a timer and at the end of this timer I will do some action. I created a timer with following code:

@interface ViewController ()

@property(nonatomic)NSTimer *timer;

@end

Then I made some check and if the audio frequency I get is < 18000 I should start the timer and wait 10 seconds to show an alert. I made this code so:

- (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"];
}

But when I receive an audio signal with a frequency < 18000, I initialize the NSTimer but it doesn't call the selector method, what's wrong in my code?

Était-ce utile?

La solution

The method signature used in scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: must have an argument for the NSTimer as it passes the timer to the method. Everything should work fine if you change

- (void)noPosition

to

- (void)noPosition:(NSTimer*)aTimer

and call it like

self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(noPosition:) userInfo:nil repeats:NO];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top