I’m working with CoreData in Cocoa (not document-based). My problem is, that I want to access the SAME NSArrayController from different NIBs. But every NIB has an own instance of this NSArrayController.
My question is now how I could generate sharedObjects (like the NSUserDefaultsController). It would help me a lot. Thanks for your answers. =)
You generally don’t want to share an NSArrayController between nibs. It’s probably better to have multiple NSArrayController (one per NIB) which are all bound to the same underlying model. If you want this model (e.g. an NSArray) to be application global, you can expose it via the NSApplication’s delegate (e.g. instantiate your custom MyAppDelegate class in MainMenu.nib and connect the NSApplication’s
delegateoutlet to the instance of your MyAppDelegate class). In other NIBs, you can then bind an NSArrayController’scontentArraybinding toShared Application.delegate.myArray(assuming MyAppDelegate exposes—via KVC-compliant methods—an NSArray binding calledmyArray). You are essentially using IB and the MainMenu.nib to create your singleton instance of MyAppDelegate.Keep in mind that this approach makes unit testing your application difficult, since there are now singletons in the object graph that you can’t mock or stub out during testing. It would be much better to create an NSWindowController or NSViewController for each secondary (non MainMenu.nib) NIB and bind the NSArrayControllers in those nibs to
File Owner.myArray. You can then instantiate the NSWindowController or NSViewController, passing it an array (or array KVC-compliant object) before loading the secondary NIB. In this way, you can test the functionality of the nibs in isolation (using a mock or stub for the array).