'comboBoxSelectionDidChange gives me previously selected value

I am using this notification for NSComboBox. Only problem is when I select a different item in the dropdown it always show previously selected value in the combo box. How can I get the currently selected value. I need to make some controls enable/disable based on the value.

- (void)comboBoxSelectionDidChange:(NSNotification *)notification {
        NSComboBox *comboBox = (NSComboBox *)[notification object];

        NSLog(@"[comboBox stringValue] : %@", [salaryBy stringValue] );
}


Solution 1:[1]

I got the selected value using:

NSString *strValue = [comboBox itemObjectValueAtIndex:[comboBox indexOfSelectedItem]];

Solution 2:[2]

I have also noticed this bug and fixed it in a different way. The correct value can be fetched when we read the value in the next run of the main run loop after the comboBoxSelectionDidChange method call as shown below

- (void)comboBoxSelectionDidChange:(NSNotification *)notification{ 

    [self performSelector:@selector(readComboValue:) withObject:[notification object] afterDelay:0];
}

- (void)readComboValue:(id)object
{
   NSString *comboValue = [(NSComboBox *)object stringValue];
   NSLog(@"%@", comboValue);
}

produces the desired result

Solution 3:[3]

I use these code with success!

Setting up:

@interface YourWindowController : NSWindowController<NSComboBoxDelegate,NSComboBoxDataSource>


- (void)windowDidLoad
{

comboBox.usesDataSource = YES;
comboBox.datasource = self;
comboBox.delegate = self;
[comboBox selectItemAtIndex:0];

}


-(void)comboBoxSelectionDidChange:(NSNotification *)notification
{

NSLog(@"Selection = %@ ",[[array objectAtIndex: (long)[comboBox indexOfSelectedItem]] objectForKey:@"yourkey"]);



}

Hope this help.

Solution 4:[4]

The problem here is that the NSComboBox selectionDidChange notification is not well documented and its actual meaning is easily misunderstood.

The notification does not mean that the field value has changed, or even that it will change, it merely means that a menu item was selected. In particular, when using keyboard navigation, the notification is triggered when using the up/down arrow keys to move through the menu but the text in the field won't actually change unless you press enter (which does not trigger a notification).

This means that attempting to use this notification by getting the object value of the selected item (as shown by @Adion) can actually result in unexpected behaviour as it doesn't necessarily reflect the field value. Using a delay to get the field value instead (as shown by @SayeedHussain) can avoid this but it won't pick up the change if applied with the enter key.

Unfortunately I haven't yet found a nice way of properly detecting changes to the combo box - as far as I know the only solution is bindings/KVO.

One possibility is forcing the menu item to be applied as soon as it's selected (comboBox.objectValue = comboBox.objectValueOfSelectedItem), but that's a change of standard UI behaviour and may not be desirable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adion
Solution 2
Solution 3 webmastx
Solution 4