基本 Cocoa 绑定:从 IB 菜单中切换布尔值

发布于 2024-08-17 07:33:41 字数 476 浏览 2 评论 0原文

我刚刚开始使用 Cocoa Bindings,虽然我已经阅读了大部分文档,但我仍然在努力实现一个基本功能,这让我怀疑我是否做错了,或者也许通过 IB 是不可能的。

目标:使用名为“切换可见性”的菜单项来切换我的应用程序控制器 (AppController) 中名为“可见性”的布尔属性的状态。我认为这应该完全可以通过IB来实现。

(注意:此处未使用 CoreData)

我认为需要发生的是我需要一个 NSObjectController,其内容出口设置为我的 AppController 类。然后我想我需要使用 Action Innvocate 绑定。但这就是我遇到麻烦的地方。

控制器键应该是选择吗?模型键路径应该是我的布尔属性的名称吗?选择器名称应该是什么?

或者我是否需要设置一个可以绑定的名为“toggleVisibility”的单独操作方法?如果我这样做,为什么我不只使用标准目标/操作关联,而不是绑定?

任何意见/方向表示赞赏。

I'm just getting started with Cocoa Bindings and while I've read through much of the documentation, I'm still struggling to implement a basic feature, making me question wether I'm doing it wrong or perhaps it's just not possible via IB.

Goal: Have a menu item called "Toggle visibility" toggle the state of a Boolean property in my application controller (AppController) called "visibility." I think this should be possible completely through IB.

(Note: CoreData is not in use here)

What I think needs to happen is I need to have an NSObjectController who's content outlet is set to my AppController class. Then I think I need to use an Action Innvocation binding. But here's where I run into trouble.

Should the Controller Key be selection? Should the Model Key Path be the name of my Boolean Property? What should Selector Name be?

Or do I need to setup a separate action method called "toggleVisibility" that I can bind to? If I did, why wouldn't I just use standard target/action associations, rather than bindings?

Any input / direction appreciated.

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

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

发布评论

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

评论(1

薄情伤 2024-08-24 07:33:41

目标:使用名为“切换可见性”的菜单项来切换我的应用程序控制器 (AppController) 中名为“可见性”的布尔属性的状态。

你的目标告诉你失败的原因。

在您对目标的描述中,您说您想要将此菜单项设置为命令、动词、祈使句——“切换可见性”。但是 这不是菜单项的正确标题

此外,使用 Bindings 实现动词菜单项本身就很困难,因为 Bindings 不是适合这项工作的工具。绑定用于在 UI 中公开属性。它不是用于执行命令的。

正确的标题由正确的基于绑定的解决方案支持。

菜单项的正确标题很简单,就是“可见”。

仅此一点就应该给您提示如何继续。这是另一个:当选择可见时,菜单项应该有一个复选标记;当它不可见时,菜单项应该没有标记。

您希望将菜单项的属性最终绑定到控制器的布尔属性。通常,菜单项的此属性为 state,但无论出于何种原因,它的绑定都被命名为 value

因此,将菜单项的 value 绑定绑定到控制器的 Boolean 属性。

Goal: Have a menu item called "Toggle visibility" toggle the state of a Boolean property in my application controller (AppController) called "visibility."

Your goal tells you why you are failing.

In your description of your goal, you say that you want to set this menu item up as a command, a verb, an imperative sentence—“toggle the visibility”. But that's not the right title for the menu item.

Moreover, implementing a verb menu item with Bindings is inherently difficult because Bindings is the wrong tool for the job. Bindings is for exposing properties in your UI. It isn't for performing commands.

The correct title is supported by the correct Bindings-based solution.

The correct title of the menu item is, simply, “Visible”.

This alone should give you a hint as to how to proceed. Here's another: When the selection is visible, the menu item should have a check mark; when it is not visible, the menu item should have no mark.

You want to bind a property of the menu item to, ultimately, a Boolean property of your controller. Normally, this property of the menu item would be state, but for whatever reason, the binding for it is named value.

So, bind the value binding of the menu item to the Boolean property of your controller.

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