Search code examples
objective-cnsbuttonnsset

Objective C - Can I use NSSet to setEnabled:NO to a set of NSButtons?


I have written a timer application that passes a speechTime from an IBAction and counts down to 0. There are five distinct speechTimes, and a toggler button that will stop the speech time countdown. I would like to be able to disable the buttons that are not the IBAction pressed while the timer is counting down (to prevent resetting the speechTime).

I currently have several [speechButton setEnable:NO] and [... setEnable:YES] calls, and they all work the way I expect; but i suspect it's poor memory management and makes for really nasty looking code. I would like to implement something like an NSSet of all the buttons, and enable / disable them using only one method so that i end up with [buttons disable]/[buttons enable].

I played around with something like this:

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];
    }

But this doesn't work. Any suggestions would be appreciated!


Solution

  • Have a look at IBOutletCollection which is defined for this situation.

    Have a look a this example.