Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在计时器后启动一个操作_Ios_Objective C_Timer - Fatal编程技术网

Ios 在计时器后启动一个操作

Ios 在计时器后启动一个操作,ios,objective-c,timer,Ios,Objective C,Timer,在我的iOS应用程序中,我必须在延迟后执行操作。我会更好地解释:我的应用程序可以识别一些音频: 18100赫兹 18200赫兹 18300赫兹 18500赫兹 为了做到这一点,我修改了。我想在设备无法识别我在应用程序中设置的频率时启动计时器。 我这样做: - (void)frequencyChangedWithValue:(float)newFrequency { frequencyRecived = newFrequency; watermarkReceived = YES

在我的iOS应用程序中,我必须在延迟后执行操作。我会更好地解释:我的应用程序可以识别一些音频:

  • 18100赫兹
  • 18200赫兹
  • 18300赫兹
  • 18500赫兹
为了做到这一点,我修改了。我想在设备无法识别我在应用程序中设置的频率时启动计时器。 我这样做:

- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;
    
    if (frequencyRecived > 18000) {
        if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"3"];
            water1 = water2 = water4 = NO;
            water3 = YES;
        }
        if (frequencyRecived >= 18450 && !water4) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"4"];
            water1 = water2 = water3 = NO;
            water4 = YES;
        }
    } else {
        
        [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"Nessuna postazione"];
        water1 = water2 = water3 = water4 = NO;
    }
}

PS:我理解你告诉我的其他问题,我将在将来解决,因为现在我需要解决计时器问题。:)

使用
NSTimer
。在
else
分支中,检查计时器是否已存在,如果不存在,则创建并计划它。在
if
的主分支中,使计时器失效并销毁

另外,您的
if
语句有许多漏洞,过于复杂。您已经检查过频率>18000,因此无需再次检查。您还可以使用
if,else if
来简化,因为每个范围都是不同的范围,如果检测到一个范围,则其他范围都不能被忽略。通过这种方式,您只需检查内部
if
是否超过上限范围

此外,在背景中设置文本看起来也是错误的。UI更新需要从主线程进行


比如:

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

    if (frequencyRecived > 18000) {

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

        if (frequencyRecived <= 18110) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        else if (frequencyRecived <= 18250) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        else if (frequencyRecived <= 18440) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"3"];
            water1 = water2 = water4 = NO;
            water3 = YES;
        }
        else {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"4"];
            water1 = water2 = water3 = NO;
            water4 = YES;
        }
    } else {
        if (self.timer == nil) {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:... target:self selector:... userInfo:nil repeats:NO];
        }

        water1 = water2 = water3 = water4 = NO;
    }
}
-(无效)频率更改,其值为:(浮点)新频率{
FrequencyReceived=新频率;
水印接收=是;
如果(接收频率>18000){
[自动定时器失效];
self.timer=nil;

如果(FrequencyReceive我更新了代码,但应用程序没有调用选择器方法。我用新代码更新了我的问题。我希望你能帮助我理解为什么它不起作用。我错过了代码中的一个检查。你为什么在后台做事情?在后台更新UI时,你可能看不到任何更新。停止这样做吧……我做到了。)我在后台更新UI,否则我无法在不停止侦听器的情况下更新它,我也无法停止它。如果我在主线程中更新UI,我必须停止侦听器,否则手机会崩溃。我看到它没有调用选择器方法…我不知道为什么…你对后台的理解是错误的。如果皮疹您还有其他问题。您必须停止在后台更新UI。
- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;

    if (frequencyRecived > 18000) {

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

        if (frequencyRecived <= 18110) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        else if (frequencyRecived <= 18250) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        else if (frequencyRecived <= 18440) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"3"];
            water1 = water2 = water4 = NO;
            water3 = YES;
        }
        else {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"4"];
            water1 = water2 = water3 = NO;
            water4 = YES;
        }
    } else {
        if (self.timer == nil) {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:... target:self selector:... userInfo:nil repeats:NO];
        }

        water1 = water2 = water3 = water4 = NO;
    }
}