NSFormatter used with a NSPopUpButton
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 NSNumberForma开发者_运维知识库tter, 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.
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.
精彩评论