2010-07-05 75 views
1

我需要有一個循環,可以不斷地檢查這個變量:目標C不斷檢查變量值

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) 
     } 


} 

} 

但是這會凍結應用程序的其餘部分。做這件事的另一種方式是什麼?

感謝, 利亞

回答

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

好的謝謝!我得到這個錯誤,但:無效的操作數到二進制> = – objectiveccoder001 2010-07-05 00:45:31

+0

@Elijah:這是從你的問題相同的代碼,除了放入一個計時器塊和定義和分配在同一行。我相信你會發布與你發佈的代碼相同的錯誤。 – icktoofay 2010-07-05 00:48:48

+0

是的,我知道。但是這個問題的解決方法是什麼? – objectiveccoder001 2010-07-05 00:49:50

3

您不應該對像這樣的文本字段的更改進行輪詢。相反,無論設置文本字段的值是否通知其他代碼,它所關心的屬性都已更改。