我编写了一个计时器应用程序,它从 IBAction 传递一个语音时间并倒计时到 0。有五个不同的语音时间,以及一个用于停止语音时间倒计时的切换按钮。我希望能够在计时器倒计时时禁用未按下 IBAction 的按钮(以防止重置语音时间)。
我目前有几个 [speechButton setEnable:NO] 和 [... setEnable:YES] 调用,它们都按我预期的方式工作;但我怀疑它的内存管理很差,而且代码看起来很讨厌。我想实现所有按钮的 NSSet 之类的东西,并仅使用一种方法启用/禁用它们,以便我最终得到 [buttons disable]/[buttons enable]。
我玩过这样的东西:
TimerViewController.h
@implement TimerViewController{
NSButton *buttonA, *buttonB, *buttonC;
}
@property (retain, readonly) NSSet *hijackableButtons;
-(void)disableButtons
@end
TimerViewController.m
...
@synthesize hijackableButtons;
-(void)init{
//blah blah blah
hijackableButtons = [NSSet setWithObjects:*buttonA,*buttonB,*buttonC,nil];
}
//...
-(void)disableButtons{
for (id buttons in hijackableButtons){
if (buttons isKindOfClass:[NSButton class]){
[buttons setEnabled:NO];
}
但这不起作用。任何建议,将不胜感激!