我做了一个有按钮的应用程序,当你按下它时,它就会被禁用,撤消操作应该会让它返回到以前的状态(启用它)。我使用NSUndoManager来实现这一点,但它不起作用。似乎我的应用程序中缺少了一些重要的东西,但我找不到确切的东西。
AppDelegate.h:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSUndoManager* undoManager;
}
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSButton *button;
- (IBAction)Disable:(id)sender;
@endAppDelegate.m:
#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我做错了什么?
发布于 2015-04-14 16:59:20
我已经编辑了你的代码,我希望我的例子能帮助你。
如果您仍然有什么问题,请通知我=)

#import "AppDelegate.h"
@interface AppDelegate ()
{
NSUndoManager* undoManager;
}
@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSButton *button;
@property (weak) IBOutlet NSButton *undoButton;
@end
@implementation AppDelegate
- (NSUndoManager*)windowWillReturnUndoManager:(NSWindow*)window
{
return undoManager;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
undoManager = [[NSUndoManager alloc]init];
[self update];
}
- (IBAction)Disable:(id)sender
{
[[undoManager prepareWithInvocationTarget:self]Enable];
[_button setEnabled:NO];
if (![undoManager isUndoing])
[undoManager setActionName:@"Disable"];
[self update];
}
- (IBAction)undo:(id)sender
{
[undoManager undo];
[self update];
}
-(void)Enable
{
[[undoManager prepareWithInvocationTarget:self]Disable:self];
[_button setEnabled:YES];
if (![undoManager isUndoing])
[undoManager setActionName:@"Enable"];
[self update];
}
- (void)update
{
self.undoButton.title = [undoManager canUndo]?@"Undo":@"Can't undo";
}
@endhttps://stackoverflow.com/questions/25103881
复制相似问题