开发者

Objective c constantly checking variable values

开发者 https://www.devze.com 2023-01-06 04:18 出处:网络
I need to have a loop that can constantly check for this variable: NSString *charlieSoundVolume; charlieSoundVolume = [charlieSoundLevel stringValue];

I need to have a loop that can constantly check for this variable:

NSString *charlieSoundVolume;
charlieSoundVolume = [charlieSoundLevel stringValue];

This variable will change by an interface builder patch. I was thinking of doing this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  while(1) {
    float floatValue;
    floatValue = 0.0001;
    NSString *charlieSoundVolume;
        c开发者_如何学PythonharlieSoundVolume = [charlieSoundLevel stringValue];
    if(charlieSoundVolume >= floatValue){
         (do something)
        }


}

}

But this will freeze the rest of the application. What is another way of doing this?

Thanks, Elijah


You should not be polling for changes to a text field like this. Instead, have whatever sets the value of the text field also notify your other code that a property it cares about has changed.


Use 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
 }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消