0

通读 Aaron Hillegass的书Cocoa Programming for Mac OS X(第三版) 我现在在第 9 章,在第 1 节的末尾Adding Undo to RaiseMan。Hillegass 告诉您向您的类添加两个方法MyDocument,然后构建应用程序以查看添加的撤消/重做并正常工作。

但是,它不起作用(为什么我会在这里?)。它说:

2012-11-27 19:55:16.231 RaiseMan[293:a0f] HIToolbox: ignoring exception 'undo: NSUndoManager 0x100188a30 is in invalid state, undo was called with too many nested undo groups' that raised inside Carbon event dispatch
(
    0   CoreFoundation                      0x00007fff88c26784 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff858ddf03 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff88c265a7 +[NSException raise:format:arguments:] + 103
    3   CoreFoundation                      0x00007fff88c26534 +[NSException raise:format:] + 148
    4   Foundation                          0x00007fff86fd0e17 -[NSUndoManager undo] + 239
    5   AppKit                              0x00007fff8976eeda -[NSApplication sendAction:to:from:] + 95
    6   AppKit                              0x00007fff8979346a -[NSMenuItem _corePerformAction] + 365
    7   AppKit                              0x00007fff897931d4 -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 121
    8   AppKit                              0x00007fff89a18cf4 -[NSMenu _internalPerformActionForItemAtIndex:] + 35
    9   AppKit                              0x00007fff898ca9e9 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 136
    10  AppKit                              0x00007fff8977599c NSSLMMenuEventHandler + 321
    11  HIToolbox                           0x00007fff83d937f7 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1002
    12  HIToolbox                           0x00007fff83d92d46 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 395
    13  HIToolbox                           0x00007fff83db0a81 SendEventToEventTarget + 45
    14  HIToolbox                           0x00007fff83ddfc35 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 387
    15  HIToolbox                           0x00007fff83e0ca0a SendMenuCommandWithContextAndModifiers + 56
    16  HIToolbox                           0x00007fff83e0c9c2 SendMenuItemSelectedEvent + 101
    17  HIToolbox                           0x00007fff83e0c8d2 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 150
    18  HIToolbox                           0x00007fff83dedc27 _ZL14MenuSelectCoreP8MenuData5PointdjPP13OpaqueMenuRefPt + 467
    19  HIToolbox                           0x00007fff83ded37c _HandleMenuSelection2 + 453
    20  AppKit                              0x00007fff89646851 _NSHandleCarbonMenuEvent + 236
    21  AppKit                              0x00007fff8961a362 _DPSNextEvent + 1908
    22  AppKit                              0x00007fff89619801 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 155
    23  AppKit                              0x00007fff895df68f -[NSApplication run] + 395
    24  AppKit                              0x00007fff895d83b0 NSApplicationMain + 364
    25  RaiseMan                            0x000000010000160a main + 33
    26  RaiseMan                            0x0000000100001218 start + 52
    27  ???                                 0x0000000000000001 0x0 + 1
)

当我单击撤消或按 cmd+z 时,在控制台中。

我在网上找到了解决方案,但它们都是关于“UndoGroups”或类似的东西。
我知道例外是关于nested undo groups,但我不使用它们(AFAIK),我什至不知道它们是什么。

我不知道我应该提供什么其他信息,所以请在评论中询问代码片段等。

4

1 回答 1

0

对于未来的读者:

Hillegass 告诉您添加 2 种方法:-insertObject:inEmployeesAtIndex:-removeObjectFromEmployeesAtIndex:. 在第二种方法中,您必须这样做:

NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index];

但我做到了:

NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self] insertObject:p atIndex:index]; //atIndex: instead of inEmployeesAtIndex:
于 2012-11-27T19:17:14.810 回答