[NSArrayController在1:many:many模型中的绑定详细信息

问题描述 投票:0回答:1

我有一个简单的程序,说明我要在实际程序中尝试做的事情。我有一对多关系,一对多关系,如果顶层有多个选择,我将无法使多个选择编辑正常工作:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

@interface ManyMany : NSObject

@property int value1;
@property int value2;

@end

@implementation ManyMany

- (id)copyWithZone:(NSZone *)zone {
    ManyMany *m = [[ManyMany allocWithZone:zone] init];
    m.value1 = self.value1;
    m.value2 = self.value2;

    return m;
}

- (BOOL)isEqual:(ManyMany *)other {
    if (other == nil) {
        return NO;
    }

    if (self.value1 == other.value1 && self.value2 == other.value2) {
        return YES;
    }

    return NO;
}

@end

@interface Many : NSObject

@property NSArray< ManyMany * > *manyManys;

@end

@implementation Many

@end

@interface One : NSObject 

@property NSArray< Many * > *manys;

@end

@implementation One

@end

@interface MyObserver : NSObject

@property NSObject *value;

@end

@implementation MyObserver

@end

int main()
{
    One *one = [[One alloc] init];
    one.manys = @[ [[Many alloc] init], [[Many alloc] init] ];
    one.manys[0].manyManys = @[ [[ManyMany alloc] init], [[ManyMany alloc] init] ];
    one.manys[1].manyManys = @[ [[ManyMany alloc] init], [[ManyMany alloc] init] ];

    NSArrayController *manyController = [[NSArrayController alloc] init];
    manyController.objectClass = [Many class];

    NSArrayController *manyManyController = [[NSArrayController alloc] init];
    manyManyController.objectClass = [ManyMany class];

    [manyController bind:NSContentArrayBinding toObject:one withKeyPath:@"manys" options:nil];
    [manyManyController bind:NSContentArrayBinding toObject:manyController withKeyPath:@"selection.manyManys" options:nil];

    MyObserver *o = [[MyObserver alloc] init];
    [o bind:@"value" toObject:manyManyController withKeyPath:@"selection.value1" options:nil];

    [manyController setSelectionIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,2)]];
    [manyManyController setSelectionIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,2)]];

    [manyManyController setValue:@(2) forKeyPath:@"selection.value1"];

    NSLog(@"contents:");
    Many *m;
    for (m in one.manys) {
        NSLog(@"many @ %p", m);
        ManyMany *mm;
        for (mm in m.manyManys) {
            NSLog(@"manyMany @ %p", mm);
            NSLog(@" %d %d", mm.value1, mm.value2);
        }
    }
}

此输出显示正确设置了第一组2,而不是全部四个。如果我改为将ManyManyController的内容绑定更改为selectedObjects.manyManys,则设置setValue:forKeyPath:将正确设置所有4,但是与观察者的绑定将崩溃。

我从根本上想念什么?

objective-c cocoa-bindings
1个回答
0
投票
  1. 您缺少用于多选的内容数组绑定。

一个索引集合,指定当contentArray或contentObject绑定返回多重选择标记时,NSArrayController视为其内容对象的项目。

[manyManyController bind:NSContentArrayForMultipleSelectionBinding toObject:manyController withKeyPath:@"[email protected]" options:nil];
  1. contentArray绑定不返回多选标记,因为Many.manys相等。开启alwaysUsesMultipleValuesMarker将解决此问题,但还将禁用Many属性的多选编辑。
manyController.alwaysUsesMultipleValuesMarker = YES;
  1. 选择全部四个四个ManyMany
[manyManyController setSelectionIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,4)]];
© www.soinside.com 2019 - 2024. All rights reserved.