Reputation: 6220
I'm writing an application for OSX (Lion) using Xcode 4.
Hitting a bit of a wall with this one and the internet has thus-far proved not particularly helpful.
I have a document defined, as well as the MainMenu.xib and a document window; The document window is of course what gets created when a document is loaded / created, and therefore has access to the relevant data.
I want to utilise Cocoa Bindings as much as possible in this project, so my question is this:
If I create another View (let's say an NSViewController with a linked Nib); how do I allow it access to the data for the document?
I've tried adding an #import for the document class, but that causes issues when I add the #import on the document class to the view controller (to create it) - I get unknown type compile errors.
I've also gone down the route of passing interim objects around (and even accessing getters / setters after creating the view controller); surely there must be a better, cleaner way?
The application has just the one NSWindowController (the default) and that is it, it's vanilla from the XCode non-core data document-based application template.
Thanks for your help, Clint
Upvotes: 1
Views: 1117
Reputation: 46020
You need to have a reference to the document of some kind in your other classes if you want to bind to it. Since you're already using an NSViewController
you can set the representedObject
of the NSViewController
to your document. Alternatively, you could subclass NSViewController
and create a custom property to refer to the document.
YourViewController* controller = [[YourViewController alloc] initWithNibName:@"YourNib" bundle:nil];
[controller setRepresentedObject:yourDocument];
In Interface Builder you can then bind objects in your view controller nib to File's Owner using a key path of representedObject.<some property of your document>
.
Upvotes: 1