Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Yashman!

macrumors newbie
Original poster
Apr 10, 2014
2
0
I made an app which has a button, when you press it, this calls Disable: method, the button becomes disabled and calling Undo action (simple cmd+Z) should return it to the previous state (enable it). I used NSUndoManager to make this possible, but it doesn't work. It seems I'm missing something essential in my app, but I can't find what exactly.
AppDelegate.h:
Code:
#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    NSUndoManager* undoManager;
}

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSButton *button;
- (IBAction)Disable:(id)sender;

@end

AppDelegate.m:
Code:
#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (NSUndoManager*)windowWillReturnUndoManager:(NSWindow*)window
{
    return undoManager;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

}

- (id) init
{
    if(self = [super init])
        undoManager = [[NSUndoManager alloc]init];
    return self;
}


- (IBAction)Disable:(id)sender
{
    [[undoManager prepareWithInvocationTarget:self]Enable];
    [_button setEnabled:NO];
    if (![undoManager isUndoing])
        [undoManager setActionName:@"Disable"];
}

-(void)Enable
{
    [[undoManager prepareWithInvocationTarget:self]Disable:self];
    [_button setEnabled:YES];
    if (![undoManager isUndoing])
        [undoManager setActionName:@"Enable"];
}
@end

What am I doing wrong?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.