Skip navigation.
 
mlReplace NSPopupButton Menu/Binding
FROM : Keith Duncan
DATE : Mon Mar 03 00:43:48 2008

I have an NSPopupButton whose menu I need to change based on a model 
property. I'm observing selection.property of the relevant 
NSArrayController to be notified when the menu needs to change. I need 
to alternate between a provided menu and a content array provided by 
another array controller.

I have it working with the code below it just seems awfully long 
winded to accomplish something seemingly simple, I was hoping someone 
could take a look at it and point out where I could improve my 
implementation.

Thanks,
Keith

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context {
    if ([[keyPath lastKeyPathComponent] 
isEqualToString:KDAlarmTypeKey]) {
       NSString *alarmNameSubpath = nil;
       if ([[object valueForKeyPath:keyPath] integerValue] == ITUNES) {
           alarmNameSubpath = KDAlarmNameKey;
           
           [advancedAlarmNameButton unbind:NSContentBinding];
           [advancedAlarmNameButton unbind:NSContentValuesBinding];
           
           [advancedAlarmNameButton removeAllItems];
           [advancedAlarmNameButton setMenu:[[sourceMenu copy] autorelease]];
       } else {
           alarmNameSubpath = KDBackupAlarmNameKey;
           
           [advancedAlarmNameButton removeAllItems];
           [advancedAlarmNameButton bind:NSContentBinding 
toObject:backupAlarmsController withKeyPath:@"arrangedObjects" 
options:nil];
           [advancedAlarmNameButton bind:NSContentValuesBinding 
toObject:backupAlarmsController withKeyPath:@"arrangedObjects.key" 
options:nil];
       }
       
       [advancedAlarmNameButton bind:NSSelectedValueBinding 
toObject:calendarController withKeyPath:[NSString 
keyPathForComponents:@"selection", alarmNameSubpath, nil] options:nil];
       
       // This is a dirty hack, setting the menu/binding the selected value 
seems to disable the selected menu item!?
       [[advancedAlarmNameButton selectedItem] setEnabled:YES];
   } else [super observeValueForKeyPath:keyPath ofObject:object 
change:change context:context];
}

Related mailsAuthorDate
No related mails found.