1

我正在构建一个 Mac OS X 应用程序,该应用程序具有一个带有单个 NSTableColumn 的 NSTableView 和一个 NSSegmentedControl。当我选择不同的段时,我打算切换表视图以显示来自不同数组控制器的行。

表列最初与IB 中self.songsArrayController的键路径绑定。arrangedObjects.name当所选段发生如下变化时,我目前正在重新绑定表列的值绑定:

- (IBAction) segmentedControlChanged:(NSSegmentedControl*) sender {
    if (sender.selectedSegment == 0) {
        [self.songNameTableColumn bind:NSValueBinding toObject:self.songsArrayController withKeyPath:@"arrangedObjects.name" options:nil];
    } else if (sender.selectedSegment == 1) {
        [self.songNameTableColumn bind:NSValueBinding toObject:self.productsArrayController withKeyPath:@"arrangedObjects.localizedTitle" options:nil];
    }
}

当我单击第二段时,我看到以下错误:

2013-08-16 08:55:26.245 MyApp[76005:303] [<SKProduct 0x10b3264d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.
2013-08-16 08:55:26.270 MyApp[76005:303] (
    0   CoreFoundation                      0x00007fff96d69b06 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8c7ea3f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff96dfdf49 -[NSException raise] + 9
    3   Foundation                          0x00007fff977ac15c -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 238
    4   Foundation                          0x00007fff976f3ea9 -[NSObject(NSKeyValueCoding) valueForKey:] + 400
    5   Foundation                          0x00007fff9771474c -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 341
    6   Foundation                          0x00007fff97714713 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 284
    7   AppKit                              0x00007fff8dbf70a4 -[NSBinder valueForBinding:resolveMarkersToPlaceholders:] + 163
    8   AppKit                              0x00007fff8dbf99f7 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 667
    9   AppKit                              0x00007fff8dbf964c -[NSValueBinder _observeValueForKeyPath:ofObject:context:] + 192
    10  AppKit                              0x00007fff8dbfe0ab -[NSTextValueBinder _observeValueForKeyPath:ofObject:context:] + 43
    11  Foundation                          0x00007fff977167b7 NSKeyValueNotifyObserver + 390
    12  Foundation                          0x00007fff977181c1 NSKeyValueDidChange + 456
    13  Foundation                          0x00007fff976d376a -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118
    14  AppKit                              0x00007fff8dc3bae6 -[NSTableRowData _addViewToRowView:atColumn:row:] + 696
    15  AppKit                              0x00007fff8dc3b63f -[NSTableRowData _addViewsToRowView:atRow:] + 151
    16  AppKit                              0x00007fff8dc39bd5 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 415
    17  AppKit                              0x00007fff8dc3995a -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 272
    18  AppKit                              0x00007fff8dc38c29 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 740
    19  AppKit                              0x00007fff8dc387c1 -[NSTableRowData updateVisibleRowViews] + 119
    20  AppKit                              0x00007fff8dc105a7 -[NSTableView layout] + 165
    21  AppKit                              0x00007fff8dbc3e65 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 112
    22  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    23  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    24  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    25  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    26  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    27  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    28  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    29  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    30  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    31  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    32  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    33  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    34  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    35  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    36  AppKit                              0x00007fff8dbc3cfe -[NSView layoutSubtreeIfNeeded] + 615
    37  AppKit                              0x00007fff8dbbf4ac -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 201
    38  AppKit                              0x00007fff8dab90a8 _handleWindowNeedsDisplayOrLayoutOrUpdateConstraints + 446
    39  Foundation                          0x00007fff976fe463 __NSFireTimer + 96
    40  CoreFoundation                      0x00007fff96d26804 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    41  CoreFoundation                      0x00007fff96d2631d __CFRunLoopDoTimer + 557
    42  CoreFoundation                      0x00007fff96d0bad9 __CFRunLoopRun + 1529
    43  CoreFoundation                      0x00007fff96d0b0e2 CFRunLoopRunSpecific + 290
    44  HIToolbox                           0x00007fff8f1c2eb4 RunCurrentEventLoopInMode + 209
    45  HIToolbox                           0x00007fff8f1c2c52 ReceiveNextEventCommon + 356
    46  HIToolbox                           0x00007fff8f1c2ae3 BlockUntilNextEventMatchingListInMode + 62
    47  AppKit                              0x00007fff8dab6533 _DPSNextEvent + 685
    48  AppKit                              0x00007fff8dab5df2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
    49  AppKit                              0x00007fff8daad1a3 -[NSApplication run] + 517
    50  AppKit                              0x00007fff8da51bd6 NSApplicationMain + 869
    51  MyApp                               0x0000000100001fc2 main + 34
    52  libdyld.dylib                       0x00007fff97d227e1 start + 0
)

看来,即使我将 NSValueBinding 重新绑定到self.productsArrayControllerkey path arrangedObjects.localizedTitle,表列仍在尝试使用旧的 key path 提取数据arrangedObjects.name。有谁知道为什么会发生这种情况,以及如何正确地将现有的 NSTableColumn 重新绑定到不同的数组控制器和键路径?

4

1 回答 1

0

-unbind:在设置新绑定之前尝试调用:

- (IBAction) segmentedControlChanged:(NSSegmentedControl*) sender {
    if (sender.selectedSegment == 0) {
        [self.songNameTableColumn unbind:NSValueBinding];
        [self.songNameTableColumn bind:NSValueBinding toObject:self.songsArrayController withKeyPath:@"arrangedObjects.name" options:nil];
    } else if (sender.selectedSegment == 1) {
        [self.songNameTableColumn unbind:NSValueBinding];
        [self.songNameTableColumn bind:NSValueBinding toObject:self.productsArrayController withKeyPath:@"arrangedObjects.localizedTitle" options:nil];
    }
}

此外,您是否验证了数组控制器都包含它们应该包含的对象。IE。确实productsArrayController包含SKProductssongsArrayController包含Song(或任何真正的类)对象?

于 2013-08-16T21:01:28.213 回答