Re: Multiple controls undo (NSTextView + NSTextField)
Re: Multiple controls undo (NSTextView + NSTextField)
- Subject: Re: Multiple controls undo (NSTextView + NSTextField)
- From: Bill Cheeseman <email@hidden>
- Date: Tue, 05 Mar 2002 09:19:24 -0500
on 02-03-05 5:38 AM, Simon Jacquier at email@hidden wrote:
>
My application is document based and my document window holds (among others)
>
a NSTextView and a NSTextField called "textLink". I have an action called
>
-myClear: to empty the content of both the textView and the textField and
>
I'd like to be able to undo it. I've RTFM but I can't figure out how to do
>
it correctly.
You should be able to take advantage of a document-based application's
built-in undo support to do this. Writing your own undo support is very
hard, as you've already discovered.
A more or less standard way to take advantage of Cocoa's built-in undo
support would be as follows, in rough outline. This assumes that you have
followed the recommended model-view-controller design pattern by separating
the data model from the views. That is, it assumes that the action method
doesn't clear the text view and the text field directly, but instead
actually clears their underlying data values, which then in turn update the
view by clearing the text view and the text field automatically.
1. The action method calls the data model's accessor methods. The action
method also sets the title of the undo and redo menu items, in anticipation
of item #2, below.
2. The accessor methods set the underlying data values to empty (@"" or 0,
or whatever). The accessor methods also register these data changes with the
undo manager. They also post notifications announcing the data change to
anybody who might be listening.
3. GUI updater methods are registered as observers of the notifications
posted in item #2, above. When they receive the notifications, they clear
the text view and the text field on the screen.
It is item #2, above, that triggers Cocoa's built-in undo mechanism. By
registering the data change with the undo manager, the rest is taken care of
for you. Fire off the action method (by clicking your Clear button, or
whatever), then look at the Edit menu and you should see "Undo Clear" or
whatever you named your undo menu item.
It is possible to write your application to undo and redo changes to the GUI
directly, as opposed to changes to the underlying data. But (except in
graphics programs and the like) it is generally considered more appropriate
to undo and redo changes to the underlying data, and let your GUI updaters
take care of changing the view to match the data. If you don't get why this
is, you haven't yet learned to think like a Cocoa object.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.