Cocoa-Binding:手动提交更改?
在我的应用程序中,我有一个绑定到 ArrayController (arrangedObjects) 的 NSTableView。我还有一个绑定到同一控制器(选择)的详细信息视图(只是一些文本字段)。
现在,每次我编辑文本字段时,更改都会自动发送到 ArrayController,表也会更改。我怎样才能避免这种情况?我想要的是一个“提交按钮”。数据更改仅应在我按下按钮时发送到控制器,而不是每次进行编辑时自动发送到控制器。
in my application I have a NSTableView bound to an ArrayController (arrangedObjects). I also have a Details-View (just some textfields) bound to the same Controller (selection).
Now every time I edit a textfield the changes are automatically send to the ArrayController and the Table changes as well. How can I avoid this? What I want is a "Submit-Button". Changes on the data should only be send to the controller when I press the button and not automatically every time I do an edit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题实际上有两个答案。第一个更哲学:在大多数情况下,您希望模型的更新立即发生。大多数情况下,用户不必为保存、提交等更改而烦恼。 Binding 与 NSUndoManager 的普遍集成意味着用户所做的任何事情都可以撤消(或者应该是不可撤消的)。所有用户操作都应该是“低风险”的,以便进行更改然后撤消不会对用户数据或应用程序状态造成不必要的“损害”。如果您将 Core Data 用于模型层,则始终可以使用
NSManagedObjectContext
的方法以编程方式回滚或保存一组更改。除非用户有确实充分的理由需要“提交”按钮,否则不要添加按钮。Gmail 的“撤消发送”功能就符合这一理念。即使发送电子邮件也应该是不可撤销的(在合理范围内)。第二个答案更实用。当然,在某些情况下,您所处理的后端系统不像 Cocoa 那样宽容地进行撤消。在这种情况下,最好的选择是创建一个临时模型对象作为 UI 的模型(想想模型-视图-视图-模型 (MVVM) 架构中的视图-模型)。当用户提交更改时,您可以将临时模型对象复制到持久模型中。在 Core Data 中,您可以使用内存中的持久存储来支持单独的托管对象上下文来保存这些临时实例,然后在提交时将此临时上下文中的更改合并到主上下文中。
There are really two answers to this question. The first is more philosophical: in most cases you want updates to the model to occur instantaneously. For the most part users shouldn't have to be bothered with saving, committing, etc. changes the make. Binding's pervasive integration with the
NSUndoManager
means that anything the user does can be undone (or should be undoable). All user actions should be "low risk" such that making a change and then undoing does not cause unnecessary "harm" to the user's data or the application state. If you're using Core Data for your model layer, you can always roll-back or save a set of changes programmatically using theNSManagedObjectContext
's methods. Unless there's a really good reason why the user needs a "Submit" button, don't put one in. In line with this philosophy is Gmail's "Undo Send" functionality. Even sending an email should be undoable (within reason).The second answer is more practical. Of course there are situations where you're dealing with a backend system that isn't as forgiving of undos as Cocoa. In that case, the best option is to create a temporary model object that serves as the model for the UI (think a View-Model in the Model-View-View-Model (MVVM) architectore). When the user submits the changes, you can copy the temporary model object into the persistent model. In Core Data, you can use an in-memory persistent store backing a separate managed object context to hold these temporary instances and then merge changes from this temporary context into your main context on submit.
这可能就足够了:
This might be enough: