I'm trying to get undo working with an application that is not document-based. So far, I think I have got the operation on the stack, but have yet to get the menu reflecting this. What I have so far is the initialisation of undoMan (in init:), but when I get the operation onto the stack (by the last method, observeValueForKeyPath:), the undo menu item does not get activated.

If I add the line:

NSLog(@"Menu item title = %@",[undoMan undoMenuItemTitle]);

I get "Menu item title = Undo Value Change", so I take this as confirmation that the action is added to the stack (or at least the setActionName is changed).

I suspect the problem is that while the operation gets added to the undo stack, the undo menu in the application is not properly hooked up to undoMan. I'm not sure how to do this (or even if this is the problem). Any help would be appreciated.

-do

[Code for AppController.m below:]

@implementation AppController

@synthesize fido;

-(id)init
{
        [super init];
        [self setValue:[NSNumber numberWithInt:5] forKey:@"fido"];
        NSNumber *n = [self valueForKey:@"fido"];

        [self addObserver:self forKeyPath:@"fido"
options:NSKeyValueObservingOptionOld context:[[NSString alloc] initWithString:@"fido has changed"]];
        
        undoMan = [[NSUndoManager alloc] init];
        return self;
}

-(IBAction)incrementFido:(id)sender
{
        [self willChangeValueForKey:@"fido"];
        fido++;
        [self didChangeValueForKey:@"fido"];
}

- (void)changeKeyPath:(NSString *)keyPath ofObject:(id)obj toValue: (id)newValue
{
        //setValue:forKeyPath: will cause the key-value observing method
        //to be called, which takes care of the undo stuff
        [obj setValue:newValue forKeyPath:keyPath];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
        NSNumber *oldValue = [change objectForKey:NSKeyValueChangeOldKey];
[[undoMan prepareWithInvocationTarget:self] changeKeyPath:keyPath ofObject:object toValue:oldValue];
        [undoMan setActionName:@"Value Change"];
        
}

@end

_______________________________________________

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

This email sent to [EMAIL PROTECTED]

Reply via email to