views:

20

answers:

2

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;
        charlieSoundVolume = [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

A: 

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
 }
}
macatomy
Ok thanks!! I get this error though: invalid operands to binary >=
Elijah W.
@Elijah: That's the same code from your question, except put into a timer block and with the definitions and assignments on the same line. You'd have gotten the same error with the code you posted, I believe.
icktoofay
Yes, I know. But what's the fix for this problem?
Elijah W.
Its impossible to tell from what you have posted so far. If you get that error, then run the app in Debug mode (with "Stop on Objective-C Exceptions") enabled, and check which line is causing it.
macatomy
if (charlieSoundVolume >= floatValue) {This is the line
Elijah W.
Here's a hint: Look at the types of the variables.
Chris Hanson
Nevermind...I figured it out! :D
Elijah W.
+3  A: 

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.

Chris Hanson