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 protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to