문제

Working through the book Cocoa Programming for Mac OS X (third edition) from Aaron Hillegass I am now at Chapter 9, at the end of section Adding Undo to RaiseMan. Hillegass tells you to add two methods to your class MyDocument and then build the app to see that undo/redo is added and working.

But, it isn't working (why else would I be here?). It says:

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
)

in the console when I click undo or press cmd+z.

I found solutions for this online, but they are all about 'UndoGroups' or something similar.
I know that the exception is about nested undo groups, but I don't use them (AFAIK), I don't even know what they are.

I don't know what else information I should give, so please ask in the comments for code fragments etc.

도움이 되었습니까?

해결책

For future readers:

Hillegass tells you to add 2 methods: -insertObject:inEmployeesAtIndex: and -removeObjectFromEmployeesAtIndex:. In the second method you have to do:

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

But I did:

NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self] insertObject:p atIndex:index]; //atIndex: instead of inEmployeesAtIndex:
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top