Hi everyone,
I'm having a bit of an issue with the undo manager and bindings which I'm sure someone here can help me out with.
In my MyDocument class I have a reference to an instance of DocumentModel, which has the following attributes:
This instance of DocumentModel has an instance of NSObjectController bound to it. I then have an NSArrayController that I bind to the 'logEntries' key of the ObjectController, in order to control the logEntries MutableArray.
This is all working perfectly well. However, I wish to override the insert and remove methods of the NSArrayController, and in the process make use of undo / redo :
I have tried putting these methods in MyDocument and in the DocumentModel objects, but whenever I add an entry nothing is put in the undo manager.
If anyone can shed any light on the issue I would much appreciate it!
I'm having a bit of an issue with the undo manager and bindings which I'm sure someone here can help me out with.
In my MyDocument class I have a reference to an instance of DocumentModel, which has the following attributes:
Code:
@interface DocumentModel : NSObject
{
double currentBalance;
double amountToSave;
double moneyToSpendPerDay;
double moneyToSpendPerWeek;
int weeksLeft;
int daysLeft;
int numberOfDays;
NSCalendarDate *endOfTermDate;
int saveCheckbox;
NSMutableArray *logEntries;
}
This instance of DocumentModel has an instance of NSObjectController bound to it. I then have an NSArrayController that I bind to the 'logEntries' key of the ObjectController, in order to control the logEntries MutableArray.
This is all working perfectly well. However, I wish to override the insert and remove methods of the NSArrayController, and in the process make use of undo / redo :
Code:
- (void)insertObject:(LogEntry *)newLog inLogEntriesAtIndex:(int)index
{
NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self]
removeObjectFromLogEntriesAtIndex:index];
NSString *undoText;
if(newLog.typeFlag == BALANCE_CHANGE_FLAG)
{
undoText = [NSString stringWithFormat:@"Balance Change"];
}
else
{
undoText = [NSString stringWithFormat:@"Add '%@'", newLog.transactionDescription];
}
if(![undo isUndoing])
{
[undo setActionName:undoText ];
}
[logEntries insertObject:newLog atIndex:index];
[self calculateBalanceAndBudget];
}
- (void)removeObjectFromLogEntriesAtIndex:(int)index
{
LogEntry *entry = [logEntries objectAtIndex:index];
NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self]
insertObject:entry inLogEntriesAtIndex:index];
NSString *undoText;
if(entry.typeFlag == BALANCE_CHANGE_FLAG)
{
undoText = [NSString stringWithFormat:@"Delete Balance Change"];
}
else
{
undoText = [NSString stringWithFormat:@"Delete '%@'", entry.transactionDescription];
}
if(![undo isUndoing])
{
[undo setActionName:undoText ];
}
[logEntries removeObjectAtIndex:index];
[self calculateBalanceAndBudget];
}
I have tried putting these methods in MyDocument and in the DocumentModel objects, but whenever I add an entry nothing is put in the undo manager.
If anyone can shed any light on the issue I would much appreciate it!