Search code examples
macosnstableviewcocoa-bindingsnsarraycontroller

Use an Array controller in different windows


I have an NSTableView in my MainMenu.xib thats bound to an array controller which in turn is bound to a mutable array in my app delegate. Everything is working correctly and I can edit the items in the tableview and add new objects to the array controller.

Now lets say I want to add another window (i.e. a new WindowController) with a tableview thats also connected to the mutable array thats in the app delegate. How would I do this?

I can pass the array on to the new window controller when I create it but my understanding is that if I add another array controller to NewWindowController.xib this will be a separate instance to the one thats in MainMenu.xib - which means that calling addObject on one array controller doesn’t update the other.

Is there some way I can keep the 2 array controllers in sync? Or is there some way to bind the tableview in the NewWindowController to the array controller in the app delegate?

Any help would be greatly appreciated.

Thanks.


Solution

  • You can create shared instance of your array controller by subclassing NSArrayControler and implementing singleton pattern or you can add it as property of your AppDelegate.