跨 Cocoa 应用程序共享数据
我已经搜索过如何正确执行此操作,但我不相信我找到了答案。这是我的设置:
NSWindowController 加载 2 个不同的 NSViewController(一次仅显示一个)。当我将文件拖放到已加载的 NSViewController 视图上时,我想保存该文件的路径。我可以获得所有这些,但我现在想要做的是,当我交换到另一个 NSViewController 时,我想将此文件路径传递给新的 NSViewController。
我想出的唯一解决方案是将通知从 NSViewController 发布到 NSWindowController,然后使用此数据初始化第二个 NSViewController,但这似乎相当复杂。有没有一种方法可以将数据保存为全局实体,然后从我的第二个 NSViewController 访问它?
注意: NSViewController 在与其他控制器交换后将被释放。
任何见解将不胜感激。
更新:我只是要更改应用程序,以便两个视图控制器始终位于内存中。这样我就可以像其他人建议的那样使用 KVO 或其他方法。
I have searched for how to correctly do this, but I don't believe I have found my answer. This is my setup:
NSWindowController loads in 2 different NSViewControllers (only one is displayed at a time). When I drop a file onto the NSViewController's view that is loaded, I want to save the path of that file. I can get all of this, but what I want to be able to do now is when I swap to my other NSViewController, I want to pass on this file path to the new NSViewController.
The only solution I have come up with is to post a notification from my NSViewController to the NSWindowController and then initialize the 2nd NSViewController with this data, but it seems rather convoluted. Is there a way that I can just save the data somewhere as a global entity and then access it later from my 2nd NSViewController?
NOTE: The NSViewControllers are being released after they are swapped with the other.
Any insight would be appreciated.
UPDATE: I'm just going to change the application so that both view controllers are in memory at all times. This way I can do as others have suggested and use KVO or other methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
视图控制器用于控制模型对象的视图。因此,您的每个视图控制器都必须从某个底层模型对象获取数据,在您的情况下,该对象可能是相同的。如果您没有,我建议您将应用程序重新设计为 MVC 模式。
创建视图控制器时,您可以设置 representedObject 属性并将文件名存储为其属性之一。如果您的模型对象符合 KVO,您甚至可以让每个视图控制器观察模型对象的文件名属性,并在发生更改时做出反应。
View controllers are meant for controlling views of model objects. So each of your view controllers must be getting their data from some underlying model object which presumably in your case is the same one. If you don't have that, I suggest you re-engineer your application to the MVC pattern.
When you create your view controllers you can set the representedObject property and store the file name as one of its properties. If your model object is KVO compliant you can even have each view controller observe the file name property of the model object and react to changes when they happen.
您可以将其保存在某个全局变量/单例中,但这也不是正确的解决方案。
一个选择是为您的第一个视图控制器提供一个指向第二个视图控制器的指针,也许作为委托(这样您的第一个控制器就不会不必要地依赖于第二个控制器)。然后它可以向其委托发送适当的消息。请注意,如果出于某种原因更合适,窗口控制器也可以是委托。
另一种选择是让第二个视图控制器而不是窗口控制器监听通知。如果第二个视图控制器不存在(或可能不存在),那么当前让窗口控制器处理通知的解决方案是非常合理的。
You could save it in a global variable/singleton somewhere, but that's not the right solution either.
One option is to give your first view controller a pointer to the second one, perhaps as a delegate (so that your first controller isn't unnecessarily dependent on the second one). Then it can just send an appropriate message to its delegate. Note that the window controller could also be the delegate if that's more appropriate for whatever reason.
Another option is to have the second view controller listen for the notification instead of the window controller. If the second view controller doesn't (or might not) exist, then your current solution of letting the window controller handle the notification is pretty reasonable.
你具体是如何进行交换的?假设您有一个导航控制器来控制将哪个视图推到顶部。为什么没有第二个视图控制器的成员来保存文件路径?
这样你就可以在视图控制器上设置这个值并将其推送到导航堆栈上进行交换。
How are you doing the swapping exactly? Say you had a Navigation Controller that's controlling which view gets pushed to the top. Why don't you have a member of the second view controller that holds the file path?
That way you can set this value on the view controller and push that onto the navigation stack to swap.