Objective c 计时器导致应用程序在某些情况下冻结
代码如下:Objective c 计时器导致应用程序在某些情况下冻结,objective-c,cocoa,nstimer,Objective C,Cocoa,Nstimer,代码如下: - (IBAction) startRecognition:(id)sender { backgroundSoundLevel = [backgroundSoundChange stringValue]; timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; } - (voi
- (IBAction) startRecognition:(id)sender {
backgroundSoundLevel = [backgroundSoundChange stringValue];
timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
- (void)timerFired:(NSTimer*)theTimer
{
NSString *charlieSoundVolume = [charlieSoundLevel stringValue];
if ([charlieSoundVolume isLessThan: backgroundSoundLevel]) {
NSRunAlertPanel(@"", charlieSoundVolume, @"", @"", @"");
}
}
所以当你按下“startRecognition”按钮时,它就会启动这个计时器循环“timer fired”。但是当charlieSoundVolume的值小于backgroundSoundLevel时,它会冻结应用程序。当它更大时,它工作得很好。这部分代码有点问题。我真的不确定是什么
背景信息:charlieSoundVolume是以NSString表示的当前卷。backgroundSoundVolume也用NSString表示。charlieSoundVolume是当前音量,backgroundSoundVolume是由NSSlider backGroundSoundChange设置的预设音量
有什么想法吗
以利亚以下是工作代码:
- (IBAction) startRecognition:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
- (void)timerFired:(NSTimer*)theTimer
{
backgroundSoundLevel = [backgroundSoundChange stringValue];
NSString *charlieSoundVolume = [charlieSoundLevel stringValue];
if ([charlieSoundVolume isLessThan: backgroundSoundLevel]) {
NSRunAlertPanel(@"", charlieSoundVolume, @"", @"", @"");
}
}
当你说“冻结应用程序”时,你的意思是面板没有显示,应用程序停止响应还是什么?是的,面板没有显示,我看到一个彩虹旋转的东西,应用程序停止响应。什么意思?我用它来表示一个NSString是否比另一个NSString小。我修复了你的标题和标签。请以后选择更多描述性标题和更合适的标签。(同时,考虑不冒充任何人的用户名。匿名性很好,模拟不是。)好的,谢谢!但那是我的真实姓名……我知道这和演员的名字一样,但我对此无能为力:DElijah Wood:一般来说,我不写示例,编写代码是你的任务;我的回答是告诉你你需要(或者应该和不应该)写什么。您需要询问NSString对象的
双
值,并使用C的小于运算符比较这些值。您可以在文档中查找第一部分(参见Xcode的“帮助”菜单),您应该已经知道第二部分了,因为您至少需要知道基本C才能有效地使用Cocoa。如果你不喜欢,我建议你读这本书:backgroundSoundLevel
是字符串,而charlieSoundLevel
是控件?您需要更清楚地命名变量。(并停止在控件中存储数据!您的控制器对象应直接拥有其中一个或两个值对象。)是。backgroundSoundLevel是NSSlider产生的值。charlieSoundLevel是NSTextField的值(quartz composer视图随修补程序更改,此quartz composer视图文件是一个仅获取声级的可视化工具模板)。有点混乱,但整个应用程序现在运行得非常完美!所以,我会仔细检查一下,让它更有效率D根据答案中的代码,charlieSoundLevel
不是NSTextField的值,而是控件(可能是文本字段)本身。