NSManagedObject 与 NSArrayController 的关系

发布于 2024-10-25 17:33:03 字数 1156 浏览 1 评论 0原文

我得到的是:

  • controller1一个NSArrayController
  • controller2一个NSArrayController。这与controller1 的属性具有父关系。
  • controller1-2Tree 一个 NSTreeController 和一个 NSOutlineView 来查看它。这显示了controller1的层次结构以及从controller2的父/子关系中找到的每个项目的子项。这是通过将两个 NSArrayController 绑定到树的值和子节点来完成的。

问题:

在我的情况下,一切都使用核心绑定。然而,与 NSTableView 不同,我的 NSOutlineView 的非正统设置意味着我当前的选择不会传递到相关的 NSArrayController 上。例如,如果我在 controller1-2Tree 中选择一个子级,它是我的 controller2 中的一个对象,但 controller2 本身并不注册选择改变。

我有相关的代码来获取选择更改。我不确定如何手动更改 controller2controller1 的当前选择项(尽管我现在需要的是 2),基于了解当前选择的项目控制器1-2树

我已经弄清楚了如何隔离当前选定的对象,只是缺少最后一步如何将其与 NSArrayController 相关联,而无需基于尝试匹配属性来迭代它。

NSManagedObject *selectedObject = [[controller1-2View itemAtRow:[controller1-2View selectedRow]] representedObject];
NSManagedObjectContext *selectedObjectContext = [selectedObject managedObjectContext];

What I've got:

  • controller1 an NSArrayController
  • controller2 an NSArrayController. This has a parent relationship to an attribute of controller1.
  • controller1-2Tree an NSTreeController and an NSOutlineView 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 two NSArrayControllers 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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2024-11-01 17:33:03

好吧,我走了一条我试图避免的艰难之路,并通过迭代对象和控制器来实现它。我确信有比这更好的方法。

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;
        }
}

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;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文