Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
Hi everybody.

I've been having a small issue getting a new feature in a program I've made to work correctly.
Currently the methods do exactly what I want them to, and when I hit undo, the action is undone as I would expect. However, when I try to redo the action, nothing happens. Hopefully this will be an easy problem to solve that I just can't see, even Aaron Hillegas says the UndoManager makes his head spin! Anyway, here's the troublesome code:
Code:
- (void)add:(double)d:(NSString *)s
{
	Log *l = [[Log alloc] init];
	[l setDescription:s];
	[l setAmount:d];
	[self doAdd:d:s:l];
	[l release];
}

- (void)addAmount:(double)d
{
	double tempBalance = [myBalance doubleValue];
	double newBalance = tempBalance + d;
	[detailsController setMyBalance:newBalance];
}

- (void)doAdd:(double)d:(NSString *)s:(Log *)l
{
	[self addAmount:d];
	[self addLog:l];
	[tableView reloadData];
	
	NSUndoManager *undo = [self undoManager];
	[[undo prepareWithInvocationTarget:self]
		removeLog:l];
	
	[[undo prepareWithInvocationTarget:self]
		subtractAmount:d];
	
	if (![undo isUndoing]) {
		[undo setActionName:@"Add to Balance"];
	}
}

- (void)subtract:(double)d:(NSString *)s
{
	Log *l = [[Log alloc] init];
	double inverseD = - d;
	[l setDescription:s];
	[l setAmount:inverseD];
	[self doSubtract:d:s:l];
	[l release];
}

- (void)subtractAmount:(double)d
{
	double tempBalance = [myBalance doubleValue];
	double newBalance = tempBalance - d;
	[detailsController setMyBalance:newBalance];
}

- (void)doSubtract:(double)d:(NSString *)s:(Log *)l
{
	[self subtractAmount:d];
	[self addLog:l];
	[tableView reloadData];
	
	NSUndoManager *undo = [self undoManager];
	[[undo prepareWithInvocationTarget:self]
		removeLog:l];
	
	[[undo prepareWithInvocationTarget:self]
		addAmount:d];
	
	if (![undo isUndoing]) {
		[undo setActionName:@"Subtract From Balance"];
	}
	
}

I hacked this code out a few nights ago, so I'm sure it's probably just a fault from trying several different approaches and maybe not thinking it through correctly.
As always any help is very much appreciated!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.