Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 15, 2007, 01:44 PM   #1
stadidas
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Simple Undo and Bindings Problem

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:

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!

Last edited by stadidas; Nov 15, 2007 at 06:34 PM.
stadidas is offline   0 Reply With Quote
Old Nov 16, 2007, 11:26 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Are you sure the NSUndoManager has been properly setup? What does [self hasUndoManager]; return?
kainjow is offline   0 Reply With Quote
Old Nov 17, 2007, 06:18 PM   #3
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Quote:
Originally Posted by kainjow View Post
Are you sure the NSUndoManager has been properly setup? What does [self hasUndoManager]; return?
Hi Kainjow,

I put the following print statement in the insertObject method of DataModel:

Code:
NSLog(@"DataModel UndoManager: %@", [self hasUndoManager]);
which printed "*** -[DocumentModel hasUndoManager]: unrecognized selector sent to instance 0x1042c60" to the console. I take it from this that I have not got NSUndoManager correctly set up for this class. How would I go about doing this?
stadidas is offline   0 Reply With Quote
Old Nov 18, 2007, 09:57 AM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Oops I thought your DocumentModel object was a NSDocument subclass. What about your [self undoManager] code. Can you post that?
kainjow is offline   0 Reply With Quote
Old Nov 18, 2007, 11:03 AM   #5
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Quote:
Originally Posted by kainjow View Post
Oops I thought your DocumentModel object was a NSDocument subclass. What about your [self undoManager] code. Can you post that?
Ah no, MyDocument keeps a reference to an instance of DocumentModel. Calling [self undoManager] on DocumentModel returns:
*** -[DocumentModel undoManager]: unrecognized selector sent to instance 0x1044fa0

Is it an acceptable form of obtaining a reference to the document undoManager to just pass a reference to the DocumentModel object at creation from MyDocument. For example to have a variable named 'undoManager' in DocumentModel and then call
Code:
[documentModel setUndoManager:[self undoManager]];
from MyDocument?
stadidas is offline   0 Reply With Quote
Old Nov 18, 2007, 11:44 AM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Ok I'm confused then.

In your original post, where are those methods being used? MyDocument or DocumentModel? If MyDocument, then the undoManager methods are fine since NSDocument maintains its own NSUndoManager, but you probably need to make sure setHasUndoManager: returns YES first. If you're calling them in DocumentModel, it will not work unless you have an undoManager method defined.
kainjow is offline   0 Reply With Quote
Old Nov 18, 2007, 12:34 PM   #7
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Quote:
Originally Posted by kainjow View Post
If you're calling them in DocumentModel, it will not work unless you have an undoManager method defined.
They are being called in DocumentModel. I have now added NSUndoManager *undoManager to DocumentModel, and pass it an undoManager from MyDocument like this:

Code:
documentModel.undoManager = [self undoManager];
I have edited the insert and remove methods to use this undoManager reference and it all works now, Cheers for your help, you've been very handy as always.

Last edited by stadidas; Nov 18, 2007 at 01:54 PM.
stadidas is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Efficiently implementing simple NSComboBox functionality with bindings Monaj Mac Programming 0 Jul 4, 2011 04:42 AM
NSMatrix simple binding question Monaj Mac Programming 1 Jun 16, 2011 08:32 AM
Erase and Install Problem, Desperate for help! imrealldum iMac 5 Mar 13, 2011 02:02 PM
Simple MBOX and PTools problem. dougens Digital Audio 1 Apr 11, 2008 11:46 AM
Core Data and Bindings Problem mduser63 Mac Programming 0 Jun 30, 2006 01:54 AM


All times are GMT -5. The time now is 05:38 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC