Re: Managing undo with NSArrayController and a table displaying its objects?
Re: Managing undo with NSArrayController and a table displaying its objects?
- Subject: Re: Managing undo with NSArrayController and a table displaying its objects?
- From: Mike Nowak <email@hidden>
- Date: Tue, 18 Nov 2003 23:54:09 -0500
I was able to solve my problem by registering for undo notifications
when I set my undo manager in my subclass of NSArrayController:
- (void)setUndoManager:(NSUndoManager*)newUndoManager
{
undoManager = newUndoManager;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(undoNotification:)
name:NSUndoManagerDidUndoChangeNotification object:undoManager];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(redoNotification:)
name:NSUndoManagerDidUndoChangeNotification object:undoManager];
}
Then in my notification methods, I reload my table if something was
undone or redone:
- (void)undoNotification:(NSNotification*)notification
{
[[self tableView] reloadData];
}
- (void)redoNotification:(NSNotification*)notification
{
[[self tableView] reloadData];
}
It's not perfect in that it stops editing the field -- it would be nice
to have the field editor still be editing but with the undone or redone
value but it will do for now.
On Nov 17, 2003, at 5:04 PM, Mike Nowak wrote:
I'm trying to understand how to handle undoing changes to the data in
my model when a table is editing it through an array controller.
Basically I have an NSArrayController (which I subclassed so I could
manage the creation of new objects) and I bound the content of the
table to the array controller using the arrangedObjects controller
key.
My array controller controls an array of custom objects containing
annual data for a company's stock. I put what I think is the proper
undo code into this model object like this:
- (void)setHighPrice:(NSNumber*)newHighPrice
{
if (highPrice != newHighPrice)
{
[[[self undoManager] prepareWithInvocationTarget:self]
setHighPrice:highPrice];
[[self undoManager] setActionName:@"Change High Price"];
[self willChangeValueForKey:@"highPrice"];
[highPrice release];
highPrice = [newHighPrice retain];
[self didChangeValueForKey:@"highPrice"];
}
}
The undo part seems to work fine. The problem is that when the user is
editing that field in the table, and then they choose Undo Change High
Price, the value doesn't update until they move to another cell.
I've tried a few things:
-- adding validateMenuItem to my array controller so I could disable
the Undo item if something is being edited in the table. This method
never gets called in my array controller though. Not sure what's going
on there.
-- I tried observing the change in highPrice key from the array
controller so I could try aborting editing in the table. Although
observeValueForKeyPath:ofObject:change:context: gets called when I
change the entry in the table by typing a new value, it doesn't get
called when the value gets changed by undo.
I am sure I am missing something simple. Any pointers would be
appreciated -- thanks!
--
Mike Nowak
Work: http://healthmedia.umich.edu/
Personal: http://snackdog.org/
"Everybody fights. Nobody quits. Otherwise I'll shoot you myself." -
Starship Troopers
_______________________________________________
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.
--
Mike Nowak
Work:
http://healthmedia.umich.edu/
Personal:
http://snackdog.org/
"Every devil I meet becomes a friend of mine." -- Indigo Girls
_______________________________________________
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.