I'd like to have a 'Detail view' for when the user double clicks on a row, I'm just not sure how to get the data to the NSViewController, so it can display the content. I'm using Core开发者_C百科Data and I can't think of a way to do this.
There are a couple of steps you need to do here:
Make sure the table view is driven by a
NSArrayController
and that your controller for the table view (either a window controller or a view controller, or whatever) has thatNSArrayController
bound to an ivar so that you access it.In your table controller set the double click action to a method in your controller and set the target to your controller.
In the double click method you can access the row by requesting the
-selectedObjects
from theNSArrayController
and that resultingNSArray
should have only one element in it.
From there you can then create your detail view or whatever else you want to do with it. You can create a sheet and display it, open a modal window, etc.
Cocoa bindings.
Apple has a great tutorial about creating Master-Detail interfaces online.
The screenshots are a bit outdated if you are using Xcode 3.2, but it's not that different.
NSViewController has a representedObject property, which allows you to bind to the model object that you are currently displaying.
Edit:
OK. My answer isn't that great because it's only half the truth.
While it shows how to bind your controls within the detail view, it does not provide a solution on how to bind the selection of the array controller to the view controller's representedObject
.
I would be interested in a bindings-only solution to this problem.
精彩评论