问题
Xcode 7, Swift 2, Mac OS X application.
I have a Storyboard with the following simple structure; WindowController containing a SplitViewController. Top panel of Split View contains a ViewController which contains a Table View. Bottom panel of Split View contains a Text Field.
My Table View is bound to an Entity in my data model using an Array Controller TableViewArrayController). The TableViewArrayController has been added to the Table View's containing View Controller. The Table View displays my data fine.
I added a second Array Controller (TextFieldArrayController) to the Split View bottom panel View Controller and bound it to the same Model Object Context as TableViewArrayController. I bound the Text Field to the TextFieldArrayController. The Text Field displays the first item in the data model but the TextFieldArrayController is not synchronising its selected property with the TableViewArrayController.
This appears to be a very simple setup and I would expect it to be not uncommon to want to display an item from the selected row in a Table View in an object which is within a different ViewController. No matter what I try I cannot make the Text Field display a property from the selected row.
If I add a Text Field to the Table View's ViewController and bind it to the same Array Controller it displays and allows me to edit a property of the selected row fine.
Can anyone suggest how I should bind a Text Field in one View Controller to the selected row of a Table View situated within a different View Controller?
回答1:
It is actually quite uncommon setup to have several instances of NSArrayController to organize master-details. You dont need TextFieldArrayController. You bind text field to the same NSArrayController as one bound to the table view (in you case it is TableViewArrayController). Here is how you set up binding for the text view:
"your data field" is the field in the model which you would like to show in the text field.
In your setup with 2 array controllers, how would you expect to sync selection? You may try to bind Selection Indexes of one array controller to the same property of second array controller...but I have never seen such setup. Not even sure it will work.
Here is example: https://github.com/emankovski/stackexchangesplitexample
UPDATE: The example shows how split view could be used instead of child controllers. Child controllers approach is also possible but requires a lot more code with no reason.
In case you do want to use more complex solution, you need custom NSSplitViewController:
class SplitViewController: NSSplitViewController {
dynamic var items = [String]()
@IBOutlet var arrayController: NSArrayController!
override func viewDidLoad() {
super.viewDidLoad()
items.append("One")
items.append("Two")
items.append("Three")
//
let master = self.splitViewItems[0].viewController as! MasterViewController
master.arrayController = self.arrayController
let detail = self.splitViewItems[1].viewController as! DetailViewController
detail.arrayController = self.arrayController
}
And then each master detail controller should have a reference to NSArrayController in the parent:
class MasterViewController: NSViewController {
dynamic var arrayController: NSArrayController?
}
And:
class DetailViewController: NSViewController {
dynamic var arrayController: NSArrayController?
}
Then you can bind master's table view to
then bind details' text edits to:
As you see a lot more actions with no reason...
来源:https://stackoverflow.com/questions/37870427/xcode-bind-a-text-field-to-the-selected-item-in-a-table-view-from-a-different-vi