ComboBoxSelectionDidChange gives me the previously selected value

I am using this notification for NSComboBox. The only problem is when I select another item from the drop-down list, it always displays the previously selected value in the combo box. How to get the current selected value. I need some controls to enable / disable based on value.

- (void)comboBoxSelectionDidChange:(NSNotification *)notification { NSComboBox *comboBox = (NSComboBox *)[notification object]; NSLog(@"[comboBox stringValue] : %@", [salaryBy stringValue] ); } 
+4
source share
3 answers

I got the selected value using:

 NSString *strValue = [comboBox itemObjectValueAtIndex:[comboBox indexOfSelectedItem]]; 
+8
source

I also noticed this error and fixed it differently. The correct value can be obtained when we read the value in the next run of the main run cycle after calling the comboBoxSelectionDidChange method, 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); } 

gives the desired result

+1
source

I use this code with success!

Setup:

 @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"]); } 

I hope for this help.

+1
source

Source: https://habr.com/ru/post/1343233/


All Articles