Search code examples
objective-ccocoamodel-bindingkey-value-observingkey-value-coding

What would be the Cocoa binding "model key path" for this textfield?


This is a focused app for learning Cocoa bindings. It is Document-based and uses Storyboards.

The initial controller is TestWindowController, which has a 'window content' relationship to TestViewController.

TestViewController has a single view in which I have placed an NSTextField called "name". This is an IBOutlet connected to the TestViewController.

The app target specifies a single Document Type, whose class is TestDocument.

My TestDocument class has a public property of NSString *name;

In IB, with the Bindings panel open in the Utilities pane, and with the NSTextField selected, I have checked the "Bind to:" and chosen "Test View Controller" from the pop-up. But in the Model Key Path field I can't seem to find a path that leads back to the "name" property of TestDocument.

I had thought that we could create a binding between a UI element and a model property. But maybe I'm going about this the wrong way? Should the Model Key Path for a UI element be to an Object Controller who in turn has it's own bindings to the document? (I can see that to be useful in a real-world sense because a single UI element might impact more than one model property, but I'm trying to keep this a little more simple than that at this point).

The purpose of the test app is to help me learn bindings in a more real-world setup (Document-based, multiple view controllers supporting views within a window) than the examples/tutorials/Q&A I've been pouring over.

I have tried to build a Model Key Path using the contextual menu that IB provides in the Model Key Path field, with things like: docu. (as I begin typing "document" the contextual menu suggests the full spelling, so I hit tab to accept the recommendation and continue editing, then when I hit the period key, the contextual menu shows me an empty menu -- I would have expected public properties on the TestDocument model to appear if I was building the correct Model Key Path. But that seemed to be the right direction, more so than other "try this" efforts:

view.window.contentViewController. which looks like a dead end. view.superview.window.windowController.docu -- nope: contextual menu does not suggest there is a document in the path of the windowController etc.

I have been reasoning it this way: I have a UI element selected so "Model Key Path" would be the path of key names leading up, over, down, to the model class. It'd be great if "document" could start the path, but I don't think a UI element has a notion of the document. The first place that knows about a document in this context is the WindowController, so to "leave" the UI element and get to my TestWindowController, I'd go

view -> superview -> window -> windowController -> document

but mine is not showing a document following the window controller in the path.

So what would the Model Key Path be?


Solution

  • You'll need to work your way up from the textview to document.

    The checkbox "Bind to:" should be set to "View Controller".

    "Model Key Path" set to: view.window.windowcontroller.document.yourmodelobject