views:

254

answers:

1

I have a NSPopUpButton whose content values are bound to an NSArray of NSNumbers. The NSPopUpButton correctly displays the array of numbers in it's popup-menu. However, when I change the selected value, I receive a message:

HIToolbox: ignoring exception 'Unacceptable type of value for attribute: property = "tempo"; desired type = NSNumber; given type = NSCFString; value = 106.' that raised inside Carbon event dispatch

Obviously, this is occurring because the object to which the selected value is bound to is expecting an NSNumber, and the NSPopUpButton is giving it an NSString. Looking through other posts, I think what I need is a NSNumberFormatter.

However, I have tried using an NSNumberFormatter, both through Interface Builder, and programatically, but the result does not change. For reference, this is the code which I have used when attempting to set the formatter of the NSPopUpButton programatically:

tempoFormatter = [[NSNumberFormatter alloc] init];
[tempoFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[tempoFormatter setFormat:@"### bpm"];

[[tempoButton cell] setFormatter:tempoFormatter];

(tempoButton is an IBOutlet connected to the NSPopUpButton)

I am wondering if using an NSNumberFormatter is even what I need? If so, what I am doing wrong? I have read through the Data Formatting Programming Guide; it seems very straight-forward, but I feel like I am missing something.

Thanks in advance.

+1  A: 

It depends on which binding you're using to get NSPopupButton's selected value.

If you're binding to "Selected Value", you'll get a NSString.

If you're binding to "Selected Object", you'll get the object represented by the selection, which in your case would be an NSNumber.

Otherwise, you would add a NSValueTransformer to your binding to transform the selected value into the object you're expecting.

Darren
Thanks Darren, your comments pointed me in the right direction. I didn't notice the difference between Selected/Content Values and Objects. After making sure I used the Objects instead of Values, I do not receive the error message any more, and the edited value is successfully stored. However, I do want to format the NSNumber value into the format "### bpm" when it is displayed in the popup-menu. I'm still having difficulties getting this to work; NSNumbers are being displayed as just that.
CJ
CJ: You should ask a separate question about that.
Peter Hosey