Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't implement redo



Hi Jeremy,

The way that redo works, is that it expects the undo manager to get a NEW set of changes WHILE the undo is being performed. It is that set of changes that is undone when you perform redo.

So redo isn't working for you because you have nothing that is registering with the undo manager while the undo is being performed.

Here's one example of code that will work:

- (void)setNoteString:(NSString *)aString;
{
NSString *currentString = [NSString stringWithString:[textView string]];
[[[self undoManager] prepareWithInvocationTarget:self] setNoteString:currentString];
[textView setString:aString];
}

- (IBAction)clearNote:(id)sender
{
[self setNoteString:@""];
[[self undoManager] setActionName:@"Clear Note"];
}

Now -clearNote: does exactly the same thing as before, except that it uses -setNoteString: for the undo action instead of doing something to the text view directly. So when you undo, -setNoteString: gets called with the old string, which registers another invocation to set the string back to blank when you redo.

A general pattern to follow is: if possible, try to make the undo invocation call the same method that did the change in the first place. This makes it much easier to get undo and redo both working correctly.

Hope this helps,
-Greg


On Tuesday, July 30, 2002, at 02:33 PM, Jeremy Dronfield wrote:

Can someone explain to me why I can't get a redo? I have an action method which clears all the text from a TextView:

- (IBAction)clearNote:(id)sender
{
NSString *currentString = [NSString stringWithString:[textView string]];
[[[self undoManager] prepareWithInvocationTarget:textView] setString:currentString];
[textView setString:@""];
[[self undoManager] setActionName:@"Clear Note"];
}

The undo works, restoring the cleared text, but redo doesn't clear it again. Is it because undo and redo are using the same value (currentString) when they invoke the -setString: method? If so, isn't that a bit stupid? The documentation gives the impression that performed undos are, as it were, turned upside down and passed over to the redo stack, but apparently not. How do I get around it?

-Jeremy

=======================================
email@hidden // email@hidden
The Alchemy Pages:
- fractious fiction at http://freespace.virgin.net/jeremy.dronfield
_______________________________________________
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.
_______________________________________________
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.

References: 
 >Can't implement redo (From: Jeremy Dronfield <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.