在单独的NIB文件中的多个视图之间共享NSArrayController [英] Sharing an NSArrayController between multiple views in separate NIB files

查看:150
本文介绍了在单独的NIB文件中的多个视图之间共享NSArrayController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,一些背景:我试图在Cocoa(对于OS X)实现主 - 细节界面。也就是说,我有一个窗口,两个 NSTableView ,显示两种不同类型的对象。对于这个问题,让我们说他们是仓库和包(选择一个类似于我的实际问题的例子)。在第一个表视图(仓库)上选择一行将显示属于该仓库的包的列表第二个表视图。对于模型部分,我目前有一个 NSMutableArray 称为仓库对象仓库,每个仓库对象有一个 NSArray 包对象。需要注意的是,仓库变量在加载NIB文件之后被修改,因此必须通知 NSArrayController



现在,我试图组织它,使master在它自己的视图对象中,detail在它自己的视图对象中。这意味着有三个NIB:WarehousesView NIB,WarehouseDetailView NIB和MainWindow NIB。




  • WarehousesView NIB包含一个

  • WarehouseDetailView NIB包含一个WarehouseDetailViewController的实例和视图本身。
  • li>
  • MainWindow NIB包含主窗口,MainWindowController的实例,以及WarehousesView和WarehouseDetailView的实例。窗口本身包含一个 NSSplitView ,并且拆分视图的视图连接到NIB文件中的相应视图实例。



这带来了我问题的前半部分:



1)查看Cocoa应用程序的应用程序视图?对我来说这是有道理的,因为在稍后一点,除了仓库清单之外,有关仓库的更多细节可能会被添加到WarehouseDetailView。



一个重要的问题,因为一切都很好,如果我跳过创建视图,将所有的控件放在窗口中直接放置一切,包括对应于仓库和包的 NSArrayController 实例相同的NIB文件。我不需要问问题的第二部分,如果我不应该这样做。



问题的第二部分基本上是: / p>

2)如果我把它拆分成了仓库和包,我应该把 NSArrayController 以便主 - 细节接口仍然工作?目前我使用Cocoa绑定,所以不知何故仓库 NSArrayController 的内容数组需要绑定到我的仓库数组,并且包的内容数组 NSArrayController 需要绑定到选择的仓库 NSArrayController



我试过几件事,我不能得到任何东西,完全工作。具体来说,我已经尝试将仓库的 NSArrayController 放入仓库视图NIB,将 NSArrayController 放入WarehouseDetailView NIB 。这种方法的问题是,我不能找出一种方法绑定包 NSArrayController 到选择的仓库 NSArrayController 。我试过的另一件事是(1)将 NSArrayController s放入MainWindow NIB,(2)连接这些 NSArrayController

提前感谢!



编辑:我看过这个相关的问题,并且他们似乎使用 NSArrayController 的每个NIB文件的单独的实例,如果我正确理解它,从设计的角度来看似乎没有意义,但是我错了?

解决方案

第1部分:肯定这样做。我会说这是一个喜好的问题。个人而言,如果视图将在窗口中同时显示,我会将它们保存在同一个nib中。*模块化也是一件好事。



第2部分:你可以把阵列控制器放在任何你喜欢的地方,真的。你唯一需要担心的是让每个对象的引用,它需要的信息,你想要它有。如果你想要我的2¢,我会说每个在视图其内容将显示在视图中的每一个。这将使你的细节视图设置更困难,但它继续你的模块化你似乎要去。 / p>

你必须记住,nib中的每个对象都是一个真实的实例。 nib为你分配和inin它们;如果你在一个nib中放置一个 MyClass 对象,在另一个nib中放置一个 MyClass 对象, 对象。这有时是一个关于nibs的棘手的事情:它是真正方便的实例自动为您创建,但它也意味着一些fiddling的引用,当你想做的事情在整个nibs。



听起来你把 WarehouseView WarehouseDetailView 的实例添加到你的个别nib和MainMenu.nib中,他们是同一个对象。它不会是这样。您必须将nib中的对象链接到他们已经知道的对象。你必须为你的特殊情况。



我不知道您的模型存储在哪里,或者如何获取nib加载。无论哪个对象它做的加载,虽然,可能是你的个人笔尖和应用程序的其余部分之间的链接。这是nibs中的文件所有者代理对象 - 它给你一个地方连接在nib中的对象,他们本来不知道的代码。



< hr>

*:如果你发现更容易布局视图,如果它们没有包含在IB中的拆分视图中,你可以自己设置它们:放置自定义视图对象在MainMenu.xib窗口中,您可以在其自己的IB窗口中打开每个视图(虽然它不会在应用程序的窗口中)。然后在分隔视图的子视图中设置 awakeFromNib


First, some background: I am trying to implement a master-detail interface in Cocoa (for OS X). That is, I have a window with two NSTableViews that display two different types of objects. For this question, let's say they are warehouses and packages (to pick an example that is analogous to my actual problem.) Selecting a row in the first table view (on a warehouse) will display a list of packages which belong to that warehouse in the second table view. For the model part, I currently have an NSMutableArray called warehouses of warehouse objects, and each warehouse object has an NSArray of package objects. One thing to note is that the warehouses variable is modified after the NIB files are loaded, so the NSArrayController has to be notified.

Now, I've tried to organize it so that the "master" is in its own view object, and the "detail" is in its own view object. This means that there are three NIBs: a WarehousesView NIB, WarehouseDetailView NIB, and a MainWindow NIB.

  • The WarehousesView NIB contains an instance of a WarehousesViewController (subclassed from NSViewController) and the view itself.
  • The WarehouseDetailView NIB contains an instance of WarehouseDetailViewController and the view itself.
  • The MainWindow NIB contains the main window, an instance of MainWindowController, and an instance of both WarehousesView and WarehouseDetailView. The window itself contains an NSSplitView, and the views of the split view are connected to the corresponding view instances in the NIB file.

That brings me to the first half of my question:

1) Is this a good way of splitting up the application views for a Cocoa application? To me it makes sense, because at a later point more details about the warehouse besides a list of its package inventory might be added to the WarehouseDetailView.

It's an important question, because everything works just fine if I skip creating views, putting all controls in the window directly and put everything else, including NSArrayController instances corresponding to Warehouses and Packages, into the same NIB file. I don't need to ask the second half of the question if I shouldn't even be doing it this way.

The second half of the question is basically:

2) Where should I place the NSArrayControllers corresponding to Warehouses and Packages if I split it up as described above so that the master-detail interface still works? Currently I am using Cocoa bindings, so somehow the content array of the Warehouse NSArrayController needs to bind to my warehouses array, and the content array of the Packages NSArrayController needs to bind to the selection of the Warehouse NSArrayController

I've tried a few things, and I couldn't get anything to work completely. Specifically, I've tried putting the NSArrayController for Warehouses into the WarehousesView NIB and the NSArrayController for Packages into the WarehouseDetailView NIB. The problem with this approach is that I cannot figure out a way to bind the Package NSArrayController to the selection of the Warehouse NSArrayController. The other thing I've tried is (1) putting both NSArrayControllers into the MainWindow NIB, (2) connecting those NSArrayControllers to IBOutlets in the MainWindowController, then (3) passing those variables to their respective view controllers via their constructors, (4) exposing them as properties in the view controllers via KVC, and (5) binding the necessary table columns in a view to the array controller through the File's Owner. The result was that nothing appeared, but there were no errors either. If one of these approaches is the preferred way to do it, I can give more details to help see if I am doing it incorrectly.

Thanks in advance!

Edit: I did look at this related question, and they seemed to be using separate instances of NSArrayControllers for each NIB file if I understood it correctly, and that didn't seem to make sense from a design point of view, but perhaps I am wrong?

解决方案

Part 1: You can certainly do this. I'd say it's a matter of preference. Personally, then, if the views were going to be displayed simultaneously in a window, I would keep them in the same nib.* Modularity is also a good thing, though.

Part 2: You can put the array controllers wherever you like, really. The only thing you need to worry about is getting each object the references that it needs to the information you want it to have. If you want my 2¢, I'd say put each in the nib with the view its contents will be displayed in. That'll make your detail view setup more difficult, but it continues the modularity you seem to be going for.

You have to remember that every object in the nib is a real instance. The nib allocates and inits them for you; if you put a MyClass object in one nib, and a MyClass object in another nib, those are two different objects. This is sometimes a tricky thing about nibs: it's really convenient to have instances automatically created for you, but it also means some fiddling around with references when you want to do things across nibs.

It sounds like you put instances of WarehouseView and WarehouseDetailView into both your individual nibs and MainMenu.nib and expected them to be the same objects. It won't be so. You have to link objects in the nibs to objects that they already know about. You'll have to work this out for your particular situation.

I don't know where your model is stored, or how you're getting the nib loaded. Whichever object it doing that loading, though, will likely be your link between the individual nib and the rest of the app. This is what the File's Owner proxy object in nibs is for -- it gives you a place to hook up objects in the nib to code that they wouldn't otherwise know about.


*: If you find it easier to layout the views if they are not enclosed in the split view in IB, you could set them up on their own: put the custom view objects in the MainMenu.xib window and you can open each view in its own IB window (though it will not be in a window in the app). Then set the split view's subviews in something's awakeFromNib.

这篇关于在单独的NIB文件中的多个视图之间共享NSArrayController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆