Reputation: 8497
Short story:
The NSArrayController's selection is being reset whenever setContent is issued. I am wondering if there is a way to turn this off.
Of course, this would be the only acceptable behaviour if I would let NSArrayController use its internal selectionIndexes, because then it wouldn't be able to keep track of both. However, the selectionIndexes are rewired as well, and this part goes off without a hitch. It still feels the need to reset the selection, though.
Update: Ugly hack solution
I've moved this to an answer. I would be pleased to see another more insightful answer though.
Long story:
I have a Cocoa Document-based Application with an inspector panel that is shared between documents (modelled after the TextEdit source code that ships with xcode). Inside the Document class I have an NSMutableArray and an NSMutableIndexSet that are linked up with bindings to an NSArrayController.
The inspector panel is in a separate nib file, and I have two identical NSArrayControllers, one from the main document window, and one from the inspector panel, so that both can interact with the document. This is why I do a manual binding to the selectionIndexes, so that I don't get two separate selections with the two separate NSArrayControllers.
The inspector panel keeps track of which document is being inspected by a:
Document *inspectedDocument;
which is updated whenever the document is switched, or no document has focus at all. An NSObjectController is linked up to inspectedDocument
, and the NSArrayController I mentioned before is linked up to that controller.
Now, my problem is that when the inspector panel is in place, and the inspectedDocument
is changed, the selection indexes are reset. The problem goes away if I don't use the inspector panel, so I assume it is its NSArrayController that is issuing this reset. I don't have any controls that bind to the selection and could change it (such as a table view).
In the inspector panel's NSArrayController, if I have "Avoid Empty Selection" ticked, the selection resets to the first object, otherwise it resets to no selection, so it is definitely a legitimate resetting of the selection. Actually, I don't even want the inspector panel to ever be able to change the selection, so ideally I would like to establish a read-only binding for that NSArrayController's selection indexes.
Upvotes: 2
Views: 819
Reputation: 8497
I still don't understand why it happens, or if it can be turned off, but the solution I am using is very simple, I just save the selection before I rewire the NSArrayController, and then restore it right after.
This is called when the inspectedDocument
is changed and the selection is reset:
NSIndexSet *indexSet = nil;
if (inspectedDocument != doc) {
indexSet = [doc.selectedIndexes copy]; // Backup selection
}
[self setValue:doc forKey:@"inspectedDocument"]; // Selection is reset here
if (indexSet) {
// The following function basically does doc.selectedIndexes = indexSet;
[inspectedDocument selectObjectsAtIndices:indexSet]; // Restore selection
[indexSet release];
}
Upvotes: 1