NSValueTransformer 没有被调用

发布于 2024-11-17 20:44:01 字数 1369 浏览 2 评论 0原文

我有我的共享用户默认 plist,看起来像:

menuItems (Array)
  Item 0 (Dictionary)
    name (String) "Menu Item 0"
    show (Boolean) NO
  Item 1 (Dictionary)
    name (String) "Menu Item 1"
    show (Boolean) YES

等等。

我有一个 NSArrayController,其内容数组绑定到共享用户默认控制器,控制器键 =“values”,模型键路径 =“menuItems”。我启用了“将内容作为复合值处理”。它的对象控制器模式是“Class”,它控制的类名是NSMutableDictionary。

期望的结果是将 NSMenuItems“隐藏”属性绑定到 plist 中的“显示”条目。不过,您会注意到,“隐藏”和“显示”是相反的,因此我创建了一个 NSValueTransformer,它的作用很简单:

return [NSNumber numberWithBool:!value];

我按如下方式设置绑定,其中“item”是一个 NSMenuItem,“valueTransformer”是一个 alloc' ed 和 init'ed 转换器如上所述,paneNum 是可以找到相应菜单项的正确字典的索引:

NSDictionary *bindingOptions = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], NSContinuouslyUpdatesValueBindingOption,
        valueTransformer, NSValueTransformerBindingOption,
        nil];
    [item bind:@"hidden"
      toObject:[[defaultsController arrangedObjects] objectAtIndex:paneNum]
   withKeyPath:@"show"
       options:bindingOptions];

绑定似乎在一定程度上起作用;菜单项最初显示或隐藏,但从未使用 valueTransformer,因此每个菜单项的“隐藏性”是向后的。 (顺便说一句,如果 plist 发生变化,菜单项“隐藏性”也不会自动更新,正如我对绑定所期望的那样。)

我错过了什么?

顺便说一句,我意识到在 plist 中存储“隐藏”而不是“显示”会更容易,但此时我想了解为什么这不起作用。

编辑:我的猜测是,这与我试图绑定到 NSMutableDictionary 内的对象有关。如果是这样的话,正确的方法是什么?

I have my shared user defaults plist that looks something like:

menuItems (Array)
  Item 0 (Dictionary)
    name (String) "Menu Item 0"
    show (Boolean) NO
  Item 1 (Dictionary)
    name (String) "Menu Item 1"
    show (Boolean) YES

and so on.

I have an NSArrayController that has its content array bound to the shared user defaults controller with the Controller Key = "values" and the Model Key Path = "menuItems". I have "Handles Content As Compound Value" enabled. It's Object Controller Mode is "Class" and the Class Name it controls is NSMutableDictionary.

The desired outcome is for NSMenuItems "hidden" property to be bound to the "show" entries in the plist. You'll notice, though, that "hidden" and "show" are opposites, so I created an NSValueTransformer that simply does:

return [NSNumber numberWithBool:!value];

I set up the binding as follows, where "item" is an NSMenuItem, "valueTransformer" is an alloc'ed and init'ed transformer as described above, and paneNum is the index where the proper dictionary can be found for the corresponding menu item:

NSDictionary *bindingOptions = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], NSContinuouslyUpdatesValueBindingOption,
        valueTransformer, NSValueTransformerBindingOption,
        nil];
    [item bind:@"hidden"
      toObject:[[defaultsController arrangedObjects] objectAtIndex:paneNum]
   withKeyPath:@"show"
       options:bindingOptions];

The binding seems to be working to a certain extent; menu items are initially shown or hidden, but the valueTransformer is never used and therefore the "hiddenness" of each menu item is backwards. (As an aside, if the plist changes, the menu items "hiddenness" is also not being updated automatically, as I would expect with bindings.)

What did I miss?

BTW, I realize that it would be easier to just store "hidden" instead of "show" in the plist, but at this point I want to understand why this isn't working.

Edit: My guess is that it has something to do with the fact that I'm trying to bind to an object inside an NSMutableDictionary. If that's the case, what's the right way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

紫罗兰の梦幻 2024-11-24 20:44:01

我刚刚结束使用 NSNegateBooleanTransformerName (我今天发现的东西)。我假设我的值转换器中有一些愚蠢的东西,或者它与尝试绑定到 NSMutableDictionary 作为我发起的示例项目有关,该项目没有使用字典工作正常。

编辑:这是我的变压器中的东西,因为 NSNegateBooleanTransformer 工作正常,即使混合了字典也是如此。 (无论如何,那里的冲突是没有意义的)。

我希望有人解释为什么基于上面代码的值转换器(应该与 NSNegateBooleanTransformer 相同)不起作用,并且我会更改我的答案接受。

I just ended up using NSNegateBooleanTransformerName (something I found today). I am going to assume there was something stupid in my value transformer or that it has to do with trying to bind to the NSMutableDictionary as a sample project I whipped up that didn't use a dictionary worked fine.

Edit: It was something in my transformer as NSNegateBooleanTransformer is working fine, even with the dictionary in the mix. (A conflict there wouldn't have made sense, anyway).

I'd love for someone to explain why a value transformer based on the code above (should be the same as NSNegateBooleanTransformer) doesn't work, and I'd change my answer acceptance.

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