I have been reading tutorials online and the Apple docs, but as someone new to writing Cocoa apps (25 years of embedded C/C++ firmware & networking code), this dark art of Cocoa bindings makes no sense to me.
1) I created a document based MacOS Cocoa app in XCode.
2) In it, I created a class called DocumentData
, subclass of NSObject
.
3) In DocumentData
are "foo" and "bar", two "dynamic" properties that are both Strings
.
4) My Document
subclass of NSDocument
contains a DocumentData
property called docData
.
5) In the makeWindowControllers()
method of my Document
class, I set the represented object of the viewController
to be its docData
property by adding the following, after self.addWindowController(windowController)
:
if let viewController = windowController.contentViewController as! ViewController? {
viewController.representedObject = self.docData
}
6) In the View Controller Scene I added two NSTextFields
to the View Controller View and called them "Foo" and "Bar" respectively.
7) I used the Assistant to ctrl drag them to the View Controller swift code window to create two IBOutlets
in the View Controller, called "foo" and "bar" respectively.
8) In the read()
method of my Document
class, I added two lines of code to set the two properties of the docData
object:
docData.foo = "foo"
docData.bar = "bar"
At this point, I think if I can get Cocoa binding set up correctly, when I open a document which has my app's file extension using "Open" from the app's "File" menu, I think a window should come up whose title is set to the name of the file I selected and where the two NSTextField
objects in the window say "foo" and "bar" respectively.
The problem is, I'm completely confused on how to do those bindings. I've seen ten different examples with different ways of doing the bindings, but nothing seems to work for me.
Am I supposed to add an NSArrayController
to the View Controller?
Am I supposed to add an NSObjectController
to the View Controller?
How exactly, in XCode's ridiculously confusing UI, am I supposed to set up exactly what bindings?
I'm sure this probably very simple to do, but I don't get it. Any help or any pointer to an online tutorial that doesn't assume far more experience with the XCode IDE than those I've found online thus far would be most appreciated.
When this works for me, I'm happy to put the code up on github somewhere and comment the heck out of it to help others struggle with XCode's nefarious IDE as it relates to these bindings.