• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Implementing undo in custom model object for lots of properties
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Implementing undo in custom model object for lots of properties


  • Subject: Implementing undo in custom model object for lots of properties
  • From: Ben Kennedy <email@hidden>
  • Date: Mon, 07 May 2012 16:11:56 -0700

Hi all,

I have a custom data model object with a number of properties of various basic types.  An array of these is managed by an NSArrayController and bound to an NSTableView (as the reader might recall from my earlier thread).  I am not using Core Data.

I am now wiring in undo support.  Were the data backed by an NSManagedObjectContext, I would get undo behaviour for free, but since there isn't, I need to write my own setters to handle it.

It seems onerous, verbose and error-prone to have to implement a custom stub setter method for every property like so:

- (void)setBlah:(Blah *)newBlah
	{
	if (newBlah != blah)
		{
		[undoManager registerUndoWithTarget:self selector:@selector(setBlah:) object:blah];
		[blah release];
		blah = [newBlah copy];
		}
	}

Thus, I have tried to be clever by doing the following override:

- (void)setValue:(id)value forKey:(NSString *)key
	{
	NSArray *undoableKeys = [NSArray arrayWithObjects: @"blah", @"foo", @"anotherProperty", nil];

	if ([undoableKeys containsObject:key])
		{
		[[undoManager prepareWithInvocationTarget:self] setValue:[self valueForKey:key] forKey:key];
		}

	[super setValue:value forKey:key];

	return;
	}

However, the undo event does not seem to be recorded -- the Undo menu command remains ghosted.  It is not clear to me why.

It wouldn't surprise me to be admonished for such an abuse of setValue:forKey:, but is there a better way?  I imagine this idiom must be extremely commonplace, but I could not find a clear directive.

I looked to Apple's "DemoMonkey" sample for its undo practices, and it just implements the verbose boilerplate for each property.  Is that really the recommended solution?

thanks,

-ben

--
Ben Kennedy, chief magician
Zygoat Creative Technical Services
http://www.zygoat.ca


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Implementing undo in custom model object for lots of properties
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: inconsistent behavior of NSString's localizedCaseInsensitiveCompare
  • Next by Date: Re: Problem parsing file in 64 bit build.
  • Previous by thread: Re: iOS + and X buttons
  • Next by thread: Re: Implementing undo in custom model object for lots of properties
  • Index(es):
    • Date
    • Thread