Delete Undo Actions for WebView NSUndoManager

I have several WebViews that I am changing and leaving my hierarchy. Some of these WebViews contain form fields, so I implement editDelegate to provide an NSUndoManager for my document.

The problem is that any typing made in WebView generates actions that are pushed onto the undo stack. After the view is removed from the hierarchy, the actions are still on the undo stack. At this point, if the user uses Cmd-Z for "Undo Typing", an exception is thrown because -undoEditing: is sent to the freed instance of WebEditorUndoTarget. (A private class used in the implementation of WebKit.)

I cannot use -removeAllActionsWithTarget: because I cannot refer to the target. It seems like the only solution is to disable deregistration for WebView.

Am I missing something or is this a limitation for WebView?

+4
source share
3 answers

One private API is invoice - [WebView _clearUndoRedoOperations]. No public API solution found yet.

+1
source

In my opinion, WebView should take responsibility for removing itself or any subordinate objects from WebView undoManager: in this case, yours.

Judging by your description, this does not do this. I suspect that your goal of finding a target so that you can explicitly remove it will be useless, so I would like to suggest another solution that I used before, or I have come far enough along this path to think about using; )

The idea of ​​this situation was to use a custom subclass of NSUndoManager, which is able to store its own excess memory of all registrations on it (by overriding registerUndoWithTarget: ... and prepareWithInvocationTarget). With a reliable array of all registrations, you can impose your own house cleaning policy at any time. That is, you can say: β€œIf the target class belongs to a structure that I don’t own, just delete it” ... or specify and say that you want to delete everything that corresponds to the WebEditorUndoTarget class, for example.

+1
source

The last time I checked NSUndoManager , there was a method - (void)removeAllActions . No need to indicate the purpose of the action.

0
source

Source: https://habr.com/ru/post/1383765/


All Articles