From: Quincey Morris <quinceymor...@earthlink.net>
Subject: Re: binding NSComboBox default selection
To: cocoa-dev <cocoa-dev@lists.apple.com>
Message-ID: <a573b8ec-7ab1-4a7f-ad9e-4c21fcf83...@earthlink.net>
Content-Type: text/plain; charset=us-ascii
> 
> 
> On Jan 7, 2010, at 09:02, Mazen M. Abdel-Rahman wrote:
> 
>> NSString * defaultSelectionValue = [myDS defaultSelectionValue];
>> 
>> NSUInteger defaultSelectionIndex =  [myDS comboBox:self 
>> indexOfItemWithStringValue:defaultSelectionValue];
>> 
>> [self selectItemAtIndex:defaultSelectionIndex];
>>              
>> [self setObjectValue:[myDS comboBox:self 
>> objectValueForItemAtIndex:defaultSelectionIndex]];
>> 
>> 'self' is a subclass of NSCombobox.  'myDS' is the datasource - which 
>> contains has information of what the default selection should be (stored in 
>> an NSString).
>> 
>> In interface builder I bound the value of the NSComboBox to a variable in a 
>> model class.
>> 
>> My problem is that it seems the model variable is not being properly 
>> informed of the change that is done when the combo box default selection is 
>> made - so that if the user closes the sheet the model variable is still 
>> 'nil'.  If the user changes the selection then it works as expected.  I 
>> tried to addd the following line when setting the default selection - 
>> thinking that maybe I had to change the NSComboBox value via key value 
>> selection:
>> 
>> [self setValue:[myDS comboBox:self 
>> objectValueForItemAtIndex:defaultSelectionIndex] 
>>                              forKey:@"value"];
>> 
>> but got the following error:
>> [<AVSComboBox 0x10047b8c0> setValue:forUndefinedKey:]: this class is not key 
>> value coding-compliant for the key value.
> 
> There are several possible problems, but not enough information here to 
> figure it out.
> 
> -- You don't say when/where this code is executed. In 'awakeFromNib'?
> 
> -- Are you sure this code is executed at all?
> 
> -- Maybe one of the lines of code returns an unexpected value. Have you 
> verified the results line by line?
> 
> -- You're using both a data source and a binding. How is that supposed to 
> work?
> 
> -- If the combo box is *bound* to your model variable (which it shouldn't 
> literally be, but rather bound to a property that's backed by the instance 
> variable), then you should rather set the model variable to the default 
> value. That's kinda the point of bindings -- avoiding glue code like the 
> above.
> 
> -- Of course your 'setValue:forKey:' failed -- NSComboBox doesn't have a 
> "value" property (though it does have a "value" binding). Its object value 
> property is called "objectValue" as you already know from using 
> 'setObjectValue:'.


Thanks for the reply Quincey - here is more detail about what I am doing.  

The code above is in 'awakeFromNib' - and it definitely is getting executed.  
The functions are all returning the proper values.

I am using the datasource for the list of items available in the comboBox .  In 
my case one of my combo boxes displays languages that a person may speak - so 
it lists English, Spanish, French, Arabic, etc.  So this data comes from the 
data source.  I am then binding the selected value to a person model that has a 
single attribute representing the person's language.  So if they select 
'Spanish' in the combo box the model's language attribute  should be  
automatically updated via bindings to indicate that the person's language is 
spanish.

Everything works perfectly if the user changes the comboBox selection - but if 
they don't do anything to it and just accept the defaults the language 
attribute of the person class is not updated.

Thanks,
Mazen
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to