Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.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
Objective c 停止无限循环目标C中的死亡风车_Objective C_Macos - Fatal编程技术网

Objective c 停止无限循环目标C中的死亡风车

Objective c 停止无限循环目标C中的死亡风车,objective-c,macos,Objective C,Macos,我正在为我的mac用objective c为自己编写一个简单的定时器程序。计时器正确地倒计时,但我得到了死亡的旋转风车。我怎样才能把风车弄走?我想这是因为我有一个无限循环,但一定有办法绕过它 我有一个iAction,它在单击按钮时触发(按钮是start)。从那里,它调用另一个函数来完成这项工作 这是我的行动: - (IBAction)timerStart:(id)sender { self.timerDidPause = NO; [self timerRunning]; } 下

我正在为我的mac用objective c为自己编写一个简单的定时器程序。计时器正确地倒计时,但我得到了死亡的旋转风车。我怎样才能把风车弄走?我想这是因为我有一个无限循环,但一定有办法绕过它

我有一个iAction,它在单击按钮时触发(按钮是start)。从那里,它调用另一个函数来完成这项工作

这是我的行动:

- (IBAction)timerStart:(id)sender {
    self.timerDidPause = NO;
    [self timerRunning];
}
下面是时间计划:

- (void)timerRunning {
for (;;) {
    usleep(1000000);
    if (self.timerDidPause == YES) {

    }
    else {
        if (self.seconds == 0) {
            if (self.minutes == 0) {
                [self timerDone];
                break;
            }
            else {
                self.seconds = 59;
                self.minutes = self.minutes - 1;
                [self formatTimerLabel:self.hours :self.minutes :self.seconds];
            }
        }
        else {
            self.seconds = self.seconds - 1;
            [self formatTimerLabel:self.hours :self.minutes :self.seconds];
        }
    }
}
}
在此函数中,调用函数formatTimerLabel,因此如下所示:

- (void)formatTimerLabel:(int)hours
                    :(int)minutes
                    :(int)seconds {
NSString *minuteString = [[NSString alloc] init];
NSString *secondString = [[NSString alloc] init];
if (minutes < 10) {
    minuteString = [NSString stringWithFormat:@"0%d", minutes];
}
else {
    minuteString = [NSString stringWithFormat:@"%d", minutes];
}
if (seconds < 10) {
    secondString = [NSString stringWithFormat:@"0%d", seconds];
}
else {
    secondString = [NSString stringWithFormat:@"%d", seconds];
}
[self.timerLabel setStringValue:[NSString stringWithFormat:@"%d:%@:%@", hours, minuteString, secondString]];
[self.timerLabel display];
}
-(void)formatTimerLabel:(int)小时
:(int)分钟
:(int)秒{
NSString*minuteString=[[NSString alloc]init];
NSString*secondString=[[NSString alloc]init];
如果(分钟<10){
minuteString=[NSString stringWithFormat:@“0%d”,分钟];
}
否则{
minuteString=[NSString stringWithFormat:@“%d”,分钟];
}
如果(秒<10){
secondString=[NSString stringWithFormat:@“0%d”,秒];
}
否则{
secondString=[NSString stringWithFormat:@“%d”,秒];
}
[self.timerLabel setStringValue:[NSString stringWithFormat:@“%d:%@:%@”,小时,分钟字符串,秒字符串];
[self.timerLabel显示];
}

您正在导致UI线程与循环挂起。几秒钟后,操作系统将光标切换到一个风车


您需要查看并计划计时器在UI线程之外运行。

在这段代码中,
UIActivityIndicator
(风车)从何而来?在调试器中,按下暂停按钮,查看代码在何处执行。是的,您不应该在UI线程中睡觉。