【问题标题】:NSFormatter used with a NSPopUpButtonNSFormatter 与 NSPopUpButton 一起使用
【发布时间】:2010-01-10 19:24:43
【问题描述】:

我有一个 NSPopUpButton,其内容值绑定到 NSNumber 的 NSArray。 NSPopUpButton 在其弹出菜单中正确显示数字数组。但是,当我更改所选值时,我会收到一条消息:

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

很明显,这是因为所选值绑定到的对象需要一个 NSNumber,而 NSPopUpButton 正在给它一个 NSString。浏览其他帖子,我认为我需要的是 NSNumberFormatter。

但是,我尝试使用 NSNumberFormatter,无论是通过 Interface Builder 还是以编程方式,但结果并没有改变。作为参考,这是我在尝试以编程方式设置 NSPopUpButton 的格式化程序时使用的代码:

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

[[tempoButton cell] setFormatter:tempoFormatter];

(tempoButton 是一个连接到 NSPopUpButton 的 IBOutlet)

我想知道使用 NSNumberFormatter 是否是我所需要的?如果是这样,我做错了什么?我已通读 Data Formatting Programming Guide;这似乎很简单,但我觉得我错过了一些东西。

提前致谢。

【问题讨论】:

  • 你是如何得到选中的对象的?您是否绑定到selectedObject

标签: cocoa formatting interface-builder cocoa-bindings nspopupbutton


【解决方案1】:

这取决于您使用哪个绑定来获取 NSPopupButton 的选定值。

如果您绑定到“选定值”,您将获得一个 NSString。

如果您绑定到“选定对象”,您将获得选择所代表的对象,在您的情况下是一个 NSNumber。

否则,您将在绑定中添加一个 NSValueTransformer 以将选定的值转换为您期望的对象。

【讨论】:

  • 谢谢达伦,您的 cmets 为我指明了正确的方向。我没有注意到选定/内容值和对象之间的区别。在确定我使用了对象而不是值之后,我不再收到错误消息,并且编辑的值已成功存储。但是,当它显示在弹出菜单中时,我确实想将 NSNumber 值格式化为“### bpm”格式。我仍然很难让它发挥作用; NSNumbers 就是这样显示的。
  • CJ:你应该问一个单独的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
相关资源
最近更新 更多