【问题标题】:NSUndoManager: redo not workingNSUndoManager:重做不起作用
【发布时间】:2012-07-02 00:13:35
【问题描述】:

我正在制作一个使用 NSSlider 的简单应用程序,可以使用两个按钮将其设置为最大值或最小值。撤消管理器应跟踪所有更改并允许撤消/重做使用这两个按钮所做的所有更改。 界面如下:

#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

以及应用截图:


撤消工作正常,但我在重做时遇到问题。

例如启动应用程序后:

  • 我点击最大化按钮。
  • 然后菜单编辑 -> 撤消放到最大值

滑块回到原来的位置。

但是如果我进入菜单Edit -> Redo put to max,滑块不会回到它的最大位置。我不明白为什么。

【问题讨论】:

    标签: objective-c cocoa nsundomanager nsslider


    【解决方案1】:

    当撤消系统执行撤消操作时,它希望您使用与撤消相同的代码注册重做操作(除了NSUndoManager 知道它正在倒带 - 但您不应该关心)。

    所以在-setSliderValue:中添加正确的NSUndoManager调用

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 2014-07-10
      • 2011-08-07
      • 2023-03-25
      • 2012-10-06
      • 1970-01-01
      相关资源
      最近更新 更多