comboBoxSelectionDidChange gives me previously sel

2019-05-05 18:56发布

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

3条回答
做自己的国王
2楼-- · 2019-05-05 19:09

I got the selected value using:

NSString *strValue = [comboBox itemObjectValueAtIndex:[comboBox indexOfSelectedItem]];
查看更多
forever°为你锁心
3楼-- · 2019-05-05 19:26

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

查看更多
一夜七次
4楼-- · 2019-05-05 19:29

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.

查看更多
登录 后发表回答