What I've got:
- controller1 an
NSArrayController
- controller2 an
NSArrayController
. This has a parent relationship to an attribute of controller1. - controller1-2Tree an
NSTreeController
开发者_JAVA百科 and anNSOutlineView
to view it. This shows the hierarchy of controller1 and the children of each item that it found from the parent/child relationship of controller2. This has been done by binding the twoNSArrayControllers
to the values and children of the tree.
The Problem:
Everything in my situation uses core-bindings. Yet, unlike an NSTableView
, the unorthodox set up of my NSOutlineView
means that my current selection isn't passed onto my relevant NSArrayController
. For example, if I select a child in my controller1-2Tree, it's an object from my controller2, but controller2 itself doesn't register the selection change.
I have the relevant code to pick up on selection changes. I'm unsure of how to manually change the current selection item of controller2 or controller1 (although it's 2 that I need right now), based on knowing the current selected item of controller1-2Tree.
I've worked out how to isolate the currently selected object, I'm just missing the last step on how to relate this to the NSArrayController
without iterating through it based on trying to match a property.
NSManagedObject *selectedObject = [[controller1-2View itemAtRow:[controller1-2View selectedRow]] representedObject];
NSManagedObjectContext *selectedObjectContext = [selectedObject managedObjectContext];
Ok, I went the hard way that I was trying to avoid and got this to work by iterating through objects and controllers. I'm sure there's a better way than this.
if ([controller1-2view parentForItem:[controller1-2view itemAtRow:[controller1-2view selectedRow]]]) {
// If not nil; then the item has a parent. If nil, it doesn't and isn't selectable.
NSManagedObject *selectedProject = [[controller1-2view itemAtRow:[controller1-2view selectedRow]] representedObject];
NSString *selectedProjectName = [selectedProject valueForKey:@"title"];
NSFetchRequest *controller2FetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *moc= [controller2 managedObjectContext];
NSEntityDescription *controller2Entity = [NSEntityDescription entityForName:@"entityTitle" inManagedObjectContext:moc];
[controller2FetchRequest setEntity:entityTitle];
NSError *controller2FetchError = nil;
newArray = [moc executeFetchRequest:controller2FetchRequest error:&controller2FetchError];
NSInteger projectCounter = 0;
[controller2FetchRequest release];
for (NSString *s in newArray) {
NSManagedObject *projectMo = [newArray objectAtIndex:projectCounter]; // assuming that array is not empty
id projectValue = [projectMo valueForKey:@"title"];
//NSLog(@"projectValue is %@ and selectedProjectName is %@", projectValue, selectedProjectName);
if (projectValue == selectedProjectName) {
//NSLog(@"Match found");
[controller2 setSelectionIndex:projectCounter];
NSLog(@"Selected in arrayController: %@", [controller2Controller selectedObjects]);
}
projectCounter = projectCounter + 1;
}
}
精彩评论