使用 Cocoa 绑定时如何取消保存首选项?

发布于 2024-08-16 05:56:12 字数 309 浏览 1 评论 0原文

我刚刚开始搞乱绑定。我已经开始实现一个首选项对话框,将一些 NSColorWell 绑定到共享默认控制器。效果很好。我的值已正确编码并保存。

然而,不起作用的是取消对话框。如果我取消,这些值仍然会保存。

似乎我应该将取消按钮绑定到 NSUserDefaultController 的“恢复”,但当然,如果我这样做,我无法将其绑定到“performClose”(除非有一种方法将按钮绑定到我想要的多个操作)我不知道)。

我应该绑定取消按钮以恢复更改并关闭窗口。显然,我可以通过编写一个同时执行这两种操作的操作方法来做到这一点,但似乎仅在 IB 中就可以实现。

I'm just starting to mess with bindings. I've started implementing a preference dialog, binding some NSColorWell's to the shared defaults controller. That's working perfectly. My values are encoded and saved correctly.

However, what's not working is canceling out of the dialog. If I cancel out, the values are still saved.

It seems like I should be binding the cancel button to the NSUserDefaultController's "revert," but of course if I do that, I can't bind it to to "performClose" (unless there's a way to bind a button to multiple actions that I'm not aware of).

What should I be binding the cancel button to both revert the changes and close the window. Obviously I could do it by writing an action method that does both, but it seems like should be possible in just IB.

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

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

发布评论

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

评论(1

凑诗 2024-08-23 05:56:12

有两件事:

首先,按照惯例,Mac OS X 上的首选项在进行更改时会立即应用。首选项更改没有“取消”或“应用”(如在 Windows 中)。有一个“默认”按钮并不罕见,它可以重置首选项,但不会取消,因为更改会立即应用。让应用程序的首选项正常工作有点奇怪,因为它不遵循约定。

其次,因为您通过绑定直接更改这些值(与使用目标/操作机制相同),所以您不会给自己留下退出更改的机会。这里最好的做法是创建一个字典控制器,其内容是应用程序首选项的副本。编辑将在该词典上进行。如果用户取消,您只需丢弃字典即可。如果用户接受更改,您可以将字典中的设置复制到实际的应用程序首选项中,然后丢弃它。如果您必须“缓冲”您的应用程序设置,这是一个很好的方法,同时仍然使用绑定将 UI 连接到首选项,但您仍然负责在用户接受时应用更改。

Two things:

First, by convention, preferences on Mac OS X are applied immediately when the change is made. There's no "cancel" or "apply" for preferences changes (as in Windows). It's not at all unusual to have a "defaults" button, which resets the preferences, but no cancel as changes are applied immediately. Making your app's preferences work otherwise is a bit weird as it doesn't follow conventions.

Second, because you're directly changing these values via bindings (same as if you'd used the target / action mechanism), you're not leaving yourself the opportunity to back out of changes. The best thing to do here is to create a dictionary controller whose contents is a copy of your app's preferences. The editing will occur on that dictionary. If the user cancels, you merely discard the dictionary. If the user accepts the changes, you'd copy the settings in the dictionary to the actual app preferences, then discard it. If you must "buffer" your app settings, this is a good way of doing it while still using Bindings for wiring the UI to the prefs, but you're still responsible for applying the changes when the user accepts.

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