简体   繁体   中英

NSArrayController too late for NSTableView datasource?

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.

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:

- (void)awakeFromNib {
    [super awakeFromNib];
    [self populateTable];
}

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.

MyManagedObject *object = [myArrayController selection];

Above code does not work. The following line of code does the trick, although it seems a bit elaborate to me..

MyManagedObject *object = [[myArrayController arrangedObjects] objectAtIndex:[myArrayController selectionIndex]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM