NSArrayController
NSArrayController
。它与controller1的属性具有父关系。NSTreeController
和NSOutlineView
可以查看它。这显示了controller1的层次结构以及它从controller2的父/子关系中找到的每个项目的子项。这是通过将两个NSArrayControllers
绑定到树的值和子项来完成的。我所处的一切都使用核心绑定。然而,与NSTableView
不同,我NSOutlineView
的非正统设置意味着我当前的选择不会传递到我的相关NSArrayController
。例如,如果我在 controller1-2Tree 中选择了一个子项,那么它就是来自 controller2 的对象,但是 controller2 本身并未注册选择改变。
我有相关的代码来了解选择更改。我不确定如何手动更改 controller2 或 controller1 的当前选择项目(尽管我现在需要的是2),基于知道当前所选的项目的 controller1-2Tree
我已经找到了如何隔离当前选中的对象,我只是错过了如何将其与NSArrayController
相关联的最后一步,而没有基于尝试匹配属性而迭代它。
NSManagedObject *selectedObject = [[controller1-2View itemAtRow:[controller1-2View selectedRow]] representedObject];
NSManagedObjectContext *selectedObjectContext = [selectedObject managedObjectContext];
答案 0 :(得分:0)
好吧,我努力避免通过迭代对象和控制器来实现这一目标。我确信有比这更好的方法。
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;
}
}