NSUndoManager:redo不工作 [英] NSUndoManager: redo not working
问题描述
我正在做一个简单的应用程序,使用NSSlider,可以把它的最大值或最小值与两个按钮。恢复管理器将跟踪所有更改,并允许撤消/重做所有更改使用这两个按钮。 br>
这里是界面:
I am making a simple application that uses a NSSlider, which can be put to it's max or min value with two buttons.The undo manager shall track all changes and allow to undo/redo all changes made using these two buttons.
Here is the interface:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
@private
NSUndoManager* undoManager;
}
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSSlider *slider;
- (IBAction)putToMax:(id)sender;
- (IBAction)putToMin:(id)sender;
- (void) setSliderValue: (float) value;
@end
实施:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize slider = _slider;
- (NSUndoManager*) windowWillReturnUndoManager: (NSWindow*) window
{
return undoManager;
}
- (IBAction)putToMax:(id)sender
{
float value= [_slider floatValue];
[ [undoManager prepareWithInvocationTarget: self] setSliderValue: value];
if(![undoManager isUndoing])
[undoManager setActionName: @"Put to Max"];
NSLog(@"%f value added to the stack",value);
[_slider setFloatValue: 100.0];
}
- (IBAction)putToMin:(id)sender
{
float value= [_slider floatValue];
[ [undoManager prepareWithInvocationTarget: self] setSliderValue: value];
if(![undoManager isUndoing])
[undoManager setActionName: @"Put to Min"];
NSLog(@"%f value added to the stack",value);
[_slider setFloatValue: 0.0];
}
- (void) setSliderValue: (float) value
{
[_slider setFloatValue: value];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
- (id) init
{
self=[super init];
if(self)
{
undoManager=[[NSUndoManager alloc]init];
}
return self;
}
@end
应用程式的萤幕撷取画面:
And a screenshot of the application:
撤消可以正常工作,但我有重做的问题。
The undo works fine, but I have problems with redo.
例如在启动应用程序后:
For example after launching the application:
- - >
滑块返回到原来的位置。
The slider goes back where it was.
但如果我转到菜单编辑 - >重做放到最大,滑块不会回到它的最大位置。
But if I go to menu Edit -> Redo put to max, the slider doesn't go back to it's max position. And I don't understand why.
推荐答案
当撤销系统执行撤销操作时,它希望您注册重做操作使用与撤消相同的代码,(除了 NSUndoManager
知道它是倒带 - 但你不应该关心)。
When the Undo system performs an undo action, it expects you to register the redo actions using the same code as for undo, (except that the NSUndoManager
knows it's rewinding - but you should not care).
因此在 -setSliderValue中添加正确的NSUndoManager调用:
这篇关于NSUndoManager:redo不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!