绑定到 NSTextField 单元格不起作用,编辑值重置为默认值

发布于 2024-08-26 19:27:40 字数 831 浏览 5 评论 0原文

我正在开发一个动态创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2024-09-02 19:27:40

事实证明,我需要实现 NSTableView 数据源方法 setObjectValue 来手动从 NSTableView (视图)获取更改,然后手动设置数组控制器(模型)中的数据,类似于下面的代码:

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    [[[entryAC arrangedObjects] objectAtIndex:rowIndex] setValue:(NSNumber *)anObject forKey:@"numberData"];
}

出于某种原因,绑定到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:

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    [[[entryAC arrangedObjects] objectAtIndex:rowIndex] setValue:(NSNumber *)anObject forKey:@"numberData"];
}

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文