Core Data View Controller binding issue
I have a document-based application with a view controller that contains a table, array controller and add/remove buttons. I have bound the Managed Object Context to File's Owner (myViewControlle开发者_运维百科r) and established property/synthesize commands for myMOC.
My issue is how to set the managed object context from within the view controller. I wrote...
NSWindow *myWindow = [[self view] window];
NSWindowController *myWindowController = [myWindow windowController];
id myDoc = [myWindowController document];
self.myMOC = [myDoc managedObjectContext];
However, calling that from within awakeFromNib
or init
, generates a nil
value for myWindow. Any help is appreciated. Thank you
I'm assuming that myView is in its own nib when you load it and then you're adding the view to a window that's created in a different nib due to myViewController being the file's owner. Is that correct? If so, [[self view] window] should be returning nil since the view hasn't been added to a window when either awakeFromNib or init get called. You would need to run myMOC set-up code after myView has been added to the window. Try breaking the above code out into a -setupMOC method and call it after you add the view to the window or superview. If you're doing it right away you might also be able to get away with calling [self performSelector:@selector(setupMOC) withObject:nil afterDelay:0.0] in awakeFromNib.
精彩评论