NSTableView中的更改不更新NSUserDefaults [英] Changes in NSTableView do not update NSUserDefaults

查看:164
本文介绍了NSTableView中的更改不更新NSUserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:


  1. 一个NSTableView填充了NSTableCellViews

  2. 每个NSTableCellView都有一个复选框,图像和文本
  3. NSTableView绑定到NSArrayController

  4. NSArrayController的内容数组绑定到NSUserDefaults中的一个字典数组

  1. An NSTableView populated with NSTableCellViews
  2. Each NSTableCellView has a checkbox, image, and text
  3. The NSTableView is bound to an NSArrayController
  4. The NSArrayController's content array is bound to an array of dictionaries in NSUserDefaults

NSTableView读取NSUserDefaults数组:显示每个复选框,图像和文本条目的正确项目数和值。但是,点击复选框不会更改保存在NSUserDefaults中的值,因此当我重新打开我的应用程序时,整个表格会重置。

The NSTableView reads the NSUserDefaults array: showing the correct number of items and value for each checkbox, image, and text entry. However, clicking on the checkboxes does not change the values saved in NSUserDefaults so the whole table resets when I reopen my app.

帖子引起了相同的问题,但他们的解决方案检查处理内容作为复合值在绑定首选项的数组控制器没有为我工作。

This SO post raised the same issue, but their solution of checking "Handles Content as Compound Value" in the binding preferences of the Array Controller did not work for me.

我错过了什么?

推荐答案

发生:

另一个答案有一个文本字段直接绑定到数组控制器。在使用 NSTableCellView 的基于视图的表视图中,有一个额外的间接层。

The other answer had a text field that was bound directly the array controller. In a view-based table view using NSTableCellView, there's an extra level of indirection.

控制器的内容数组被分配给表单元格视图的 objectValue 。然后,您的复选框被绑定到表单元格视图的 objectValue 属性。假设元素是 NSMutableDictionary 对象。因此, objectValue 是一个 NSMutableDictionary 。切换复选框将更改字典中的值之一。但这不同于在表单元视图上调用 -setObjectValue:。因此,表单元格视图的绑定没有理由通过绑定到数组控制器来转发新的对象值。

The elements of the array controller's content array get assigned to the table cell view's objectValue. Then, your checkbox is bound to the objectValue property of the table cell view. Suppose the elements are NSMutableDictionary objects. So, objectValue is an NSMutableDictionary. Toggling the checkbox changes one of the values in the dictionary. But that's not the same as calling -setObjectValue: on the table cell view. So, the table cell view's binding has no reason to forward the new object value through its binding to the array controller.

>从复选框到 NSTableCellView objectValue 的绑定上的设置处理内容作为复合值将强制它在表单元视图上调用 -setObjectValue:,然后它将通过绑定到数组控制器转发新值。这将是除了将数组控制器绑定到用户默认控制器上设置处理内容作为复合值。

I think that setting "Handles Content As Compound Value" on the binding from the checkbox to the NSTableCellView's objectValue will force it to call -setObjectValue: on the table cell view, which will then forward the new value through the binding to the array controller. This would be in addition to setting "Handles Content As Compound Value" on the array controller's binding to the user defaults controller.

我认为你需要一个 NSTableCellView 的自定义子类为该复选框的属性。将复选框绑定到此属性,而不是通过 objectValue 。 getter将获得 objectValue 的相关属性(在本例中,我将使用允许属性)。设置器实际上将修改复合对象,然后将其分配给 objectValue 属性,以强制绑定将其写回数组控制器。例如:

I think you will need a custom subclass of NSTableCellView with a property for the checkbox. Bind the checkbox to this property instead of going through objectValue. The getter would obtain the relevant property (for this example, I'll use an allowed property) of the objectValue. The setter would actually modify the compound object and then assign it to the objectValue property to force the binding to write it back to the array controller. So, something like:

@property (getter=isAllowed, nonatomic) BOOL allowed;

...

- (BOOL) isAllowed
{
    MyModelClass* model = self.objectValue;
    return model.allowed;
}

- (void) setAllowed:(BOOL)allowed
{
    MyModelClass* model = self.objectValue;
    model.allowed = allowed;
    self.objectValue = model;
}

// Make sure KVO and bindings know that when objectValue changes that means that allowed changes, too.
+ (NSSet*) keyPathsForValuesAffectingAllowed
{
    return [NSSet setWithObject:@"objectValue.allowed"];
}

这篇关于NSTableView中的更改不更新NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆