NSArrayController 对于 NSTableView 数据源来说太晚了?
所以, 我实例化一个 NSWindowController,它又实例化一个 .xib。 xib 有 NSArrayControllers,它用 NSManagedObjects 填充它们的数组。 窗口控制器有一个(可变的)数组属性,它充当 .xib 中 NSTableView 的数据源。我已经实现了 NSTableViewDataSource (和委托)方法。 到目前为止,一切顺利……
窗口控制器具有连接 xib 中阵列控制器的出口。当我尝试填充数组时(使用字典,对象的键与表的列标识符相对应,没有问题),我很难获取数组控制器的内容。
不知何故,我没有让数组控制器及时获取其托管对象。当我获取数组控制器的数组 (arrangedObjects:) 来创建字典时,对于填充表的数组属性,我什么也得不到。
我没有按照正确的顺序做事吗?有人可以给我一个很好的解释,说明 IB 对象(例如数组控制器)如何以及何时获取数据?我应该将表填充代码移至界面生成器中的(子类化)对象吗? 我尝试在窗口控制器的 init、awakeFromNib 和 windowDidLoad 方法中操作表的数组。虽然这似乎有效,但我怀疑是否必须将表的数组对象添加到窗口的 makeKeyAndOrderFront 方法中。
So,
I instantiate an NSWindowController, which in turn instantiates a .xib.
The xib has NSArrayControllers, which populate their array with NSManagedObjects.
The window controller has an (mutable) array property, which serves as datasource for an NSTableView in the .xib. I have the NSTableViewDataSource (and delegate) methods implemented.
So far, so good..
The window controller has outlets to the array controllers in the xib. When I try to fill the array (with dictionaries, the keys of the objects correspond with the column identifiers of the table, no problem there), I am having a really hard time getting the content of the array controllers.
Somehow, I am not getting the array controllers to fetch their managed objects in time. When I get the array (arrangedObjects:) of the array controllers, to create dictionaries with, for the array property that fills the table, I get nothing.
Am I not doing things in the correct order? Could someone point me to a good explanation how and when IB-objects (such as array controllers) fetch their data? Should I move the table populating code to a (subclassed) object in the interface builder?
I have tried manipulating the table's array in the window controller's init, awakeFromNib, and windowDidLoad methods. Although this seems to work, I doubt I'd have to put the table's array object adding in the window's makeKeyAndOrderFront method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的。
看来 windowController 的 init 和 windowDidLoad 方法是填充表数组的错误位置。但我并不完全确定为什么。
然而,当我等待 windowController 的 awakeFromNib 完成时,一切似乎都工作正常,如下所示:
populateTable 方法然后使用 IBOutlet 实例变量,这些变量在 .xib 中从文件所有者(我的窗口控制器)连接,到保存 NSManagedObjects 的 NSArrayControllers。
在 xib 中按层次结构排序较深的元素的 awakeFromNib 方法是否会比 xib 结构中较高层元素的 awakeFromNib 触发得更快?
我还遇到了一些麻烦,发现数组控制器的选择返回 NSObjectControllerProxy,而不是实际选择的托管对象。
上面的代码不起作用。
下面的代码行就达到了目的,尽管对我来说似乎有点复杂。
Right.
It seems that the windowController's init and windowDidLoad methods are the wrong places to populate the table's array. I'm not entirely sure why though.
However, everything seems to work okay when I wait for the awakeFromNib of the windowController to finish, like so:
The method populateTable then uses the IBOutlet instance variables, which are connected in the .xib, from the File's owner (my window controller), to NSArrayControllers which hold NSManagedObjects.
Could it be that the awakeFromNib method of elements that are ordered hierarchically deeper in a xib, fire sooner than the awakeFromNib from elements higher up in a xib's structure?
I've also had a bit of trouble discovering that an array controller's selection returns an NSObjectControllerProxy, instead of the actual selected managed object.
Above code does not work.
The following line of code does the trick, although it seems a bit elaborate to me..