Objective c不断检查变量值

时间:2010-07-05 00:35:33

标签: objective-c xcode nsstring

我需要一个可以不断检查这个变量的循环:

NSString *charlieSoundVolume;
charlieSoundVolume = [charlieSoundLevel stringValue];

此变量将由界面构建器修补程序更改。我在想这样做:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  while(1) {
    float floatValue;
    floatValue = 0.0001;
    NSString *charlieSoundVolume;
        charlieSoundVolume = [charlieSoundLevel stringValue];
    if(charlieSoundVolume >= floatValue){
         (do something)
        }


}

}

但是这会冻结应用程序的其余部分。这样做的另一种方式是什么?

谢谢, 以利亚

2 个答案:

答案 0 :(得分:3)

您不应该轮询对此类文本字段的更改。相反,如果设置了文本字段的值,也会通知您的其他代码它关注的属性已经更改。

答案 1 :(得分:1)

使用NSTimer

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
 timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

- (void)timerFired:(NSTimer*)theTimer
{
 float floatValue = 0.0001;
 NSString *charlieSoundVolume = [charlieSoundLevel stringValue];
 if (charlieSoundVolume >= floatValue) {
  // do something
 }
}