目标C不断检查变量值
问题描述:
我需要有一个循环,可以不断地检查这个变量:目标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)
}
}
}
但是这会冻结应用程序的其余部分。做这件事的另一种方式是什么?
感谢, 利亚
答
使用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
}
}
答
您不应该对像这样的文本字段的更改进行轮询。相反,无论设置文本字段的值是否通知其他代码,它所关心的属性都已更改。
好的谢谢!我得到这个错误,但:无效的操作数到二进制> = – objectiveccoder001 2010-07-05 00:45:31
@Elijah:这是从你的问题相同的代码,除了放入一个计时器块和定义和分配在同一行。我相信你会发布与你发布的代码相同的错误。 – icktoofay 2010-07-05 00:48:48
是的,我知道。但是这个问题的解决方法是什么? – objectiveccoder001 2010-07-05 00:49:50