绑定到 NSTextField 单元格不起作用,编辑值重置为默认值
我正在开发一个动态创建 NSTableColumns 的核心数据文档应用程序。数据单元格类型可以是复选框、滑块等。以编程方式绑定到所有单元格类型都可以,但 NSTextFieldCell
除外。
所有 NSTextFieldCells
绑定失败,编辑后返回默认值。无论它们绑定到字符串、数字(应用了 NSNumberFormatter
)还是日期(应用了 NSDateFormatter
),都会发生这种情况。我使用以下格式来执行所有绑定:
NSDictionary *textFieldOpts = [NSDictionary dictionaryWithObjectsAndKeys:@"YES", NSContinuouslyUpdatesValueBindingOption, @"YES", NSValidatesImmediatelyBindingOption, nil];
[aCell bind:@"value" toObject:[[entryAC arrangedObjects] objectAtIndex:0] withKeyPath:@"numberData" options:textFieldOpts];
同样,如果单元格类型不是 NSTextFieldCell
,则这些语句将起作用。
我投入了一个 -observeValueForKeyPath
方法来记录值何时发生变化...对于其他单元格类型(例如 NSSliderCell
),我可以看到值发生变化,但是随着NSTextFieldCell
,它永远不会更新。
I'm working on a Core Data document application that dynamically creates NSTableColumns
. The data cell type may be a checkbox, slider, etc. Programmatically binding to all cell types works, except for NSTextFieldCell
.
All NSTextFieldCells
fail to bind, and after editing they return to their default value. This happens no matter if they're binding to a string, a number (with an NSNumberFormatter
applied), or a date (NSDateFormatter
applied). I'm using the following format to do all bindings:
NSDictionary *textFieldOpts = [NSDictionary dictionaryWithObjectsAndKeys:@"YES", NSContinuouslyUpdatesValueBindingOption, @"YES", NSValidatesImmediatelyBindingOption, nil];
[aCell bind:@"value" toObject:[[entryAC arrangedObjects] objectAtIndex:0] withKeyPath:@"numberData" options:textFieldOpts];
Again, these statements work if the cell type is anything but an NSTextFieldCell
.
I threw in an -observeValueForKeyPath
method to log when the value changes... and for other cell types (NSSliderCell
for instance) I can see the value changing, but with the NSTextFieldCell
, it never, ever updates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我需要实现 NSTableView 数据源方法 setObjectValue 来手动从 NSTableView (视图)获取更改,然后手动设置数组控制器(模型)中的数据,类似于下面的代码:
出于某种原因,绑定到NSTextFieldCells,当以编程方式逐个单元格设置时,只能以一种方式工作:仅显示数据。就我个人而言,我会将其归类为错误......
Turns out that I needed to implement the NSTableView data source method setObjectValue to manually get the change from the NSTableView (View) and then manually set the data in the array controller (Model) similar to the code below:
For some reason, the bindings to NSTextFieldCells, when set programmatically on a cell-by-cell basis, only work one way: to display the data only. Personally I would classify this as a bug...