NSUndoManager : separate multiple changes in one run loop cycle

…衆ロ難τιáo~ 提交于 2020-01-14 13:35:32

问题


I'm using [undoManager registerUndoWithTarget::] to add some changes to undo stack. However, sometimes happens, when in one run loop cycle two changes added to the same group, so they are reverted at once, which is not behavior I'd like to have. I want to separate these two changes to have two items in undo stack. How to correctly implement this? Use [NSObject performSelector: ] to call second undo addition in the next run loop cycle, or whatever else?

Thanks.


回答1:


As you’ve noticed, by default NSUndoManager automatically groups undo operations in one run loop cycle. You can change that behaviour, though: -[NSUndoManager setGroupsByEvent:] will disable automatic grouping if you send a NO argument. Note that you need to make sure that all groups are closed before the undo manager executes -undo or -undoNestedGroup. Since other Cocoa classes may want to register undo operations without explicitly creating a group, you can disable automatic grouping right before you register undo groups, reenabling after you’ve registered those groups.

For example:

- (void)someMethod {
    NSUndoManager *undoManager = …; // for example, [[self window] undoManager]
    [undoManager setGroupsByEvent:NO];
    {
        [undoManager beginUndoGrouping];
        {
            [undoManager registerUndoWithTarget:modelObject selector:@selector(setString1:) object:[modelObject string1]];
            [modelObject setStringValue:newValue1];
            [undoManager setActionName:@"String 1 Change"];
        }
        [undoManager endUndoGrouping];


        [undoManager beginUndoGrouping];
        {
            [undoManager registerUndoWithTarget:modelObject selector:@selector(setString2:) object:[modelObject string2]];
            [modelObject setString2:newValue3];

            [undoManager registerUndoWithTarget:modelObject selector:@selector(setString3:) object:[modelObject string3]];
            [modelObject setString3:newValue3];

            [undoManager setActionName:@"Strings 2 and 3 Change"];
        }
        [undoManager endUndoGrouping];
    }
    [undoManager setGroupsByEvent:YES];
}

In -someMethod, three changes are applied to modelObject, modifying its string1, string2 and string3 properties. The change applied to string1 is part of an undo group and the changes to string2 and string3 are part of another undo group. Both groups are enclosed in a block where the undo manager isn’t grouping all operations in the default undo group for the current run loop cycle. After this method is executed, the first undo operation undoes both string2 and string3 changes and subsequent undo operation undoes the change applied to string1, provided there wasn’t another undo group enclosing them.

I used C blocks {} to make these two hierarchies (no grouping by events and undo groups) clear.

NB: NSUndoManager is not thread-safe.



来源:https://stackoverflow.com/questions/16763716/nsundomanager-separate-multiple-changes-in-one-run-loop-cycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!