On May 8, 2015, at 02:46 , Charles Jenkins <cejw...@gmail.com> wrote:
> 
> I may have a fundamental misunderstanding of how a document class, a text 
> view, and an undo manager work together.

It depends a bit on what kind of editing your document can have done to it.

> I already tried plugging the document’s undoManager into the text view after 
> the NIB loads, but the text view’s undoManager property isn’t directly 
> settable.

If you wanted a text view to use the document’s undo manager, you *could* 
implement the ‘windowWillReturnUndoManager:’ NSWindowDelegate method to return 
it. That is, you would do this in your window controller, or in your NSDocument 
subclass if you aren’t using a window controller, whichever is hooked up as the 
window delegate.

I’m not sure, though, that this is what’s generally wanted. If you do so, then 
the keystroke undo for every text view and text field will live in your 
document’s undo chain, and that almost certainly isn’t what you want.

Undo for text fields (and text views when the content isn’t directly your 
document content) is usually handled by the window-provided freestanding undo 
manager. In document-based apps, changes to the model are generally handled by 
end-editing notifications, delegate methods or bindings, and uncommitted text 
changes are dealt with via the “informal” NSEditor and NSEditorRegistration 
protocols. (Note that NSViewController and NSDocument implement support for 
these, but NSWindowController doesn’t, which is a big PITA.) In these 
scenarios, a separate window-specific undo manager is all you need.

> As a text editor, shouldn’t my app have just one undo manager for each 
> Document, one that works for the document window and the text view contained 
> within it? If so, how do I configure that?

In this case, the text view should probably use the document undo manager, 
though you may have to do extra work to coordinate its use with your document’s 
needs. To configure it, you should tell the text view what its undo manager is, 
via its ‘undoManagerForTextView:’ delegate method.

I think that if you supply an undo manager this way, the text view won’t ask 
the window for its undo manager, so you avoid the 
‘windowWillReturnUndoManager:’ process and the danger of getting tangled up 
with undo actions for other editing controls in the same window.



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to