NSUndoManager只撤消,而不是重做

时间:2014-06-19 17:44:54

标签: ios objective-c

我根据NSUndoManager的示例代码编写简单的撤消和重做更改背景颜色。 所有事情都适用于撤消操作。但是对于Redo,它不会跳转到之前注册的NSUndoManager的注册方法。 请遵守代码:

#import "ViewController.h"
enum color {kWhite = 0, kRed, kOrange, kYellow, kGreen, kCyan, kBlue, kMagenta};
@interface ViewController (){
  enum color kColor;
  NSArray * colorArr;
  NSUndoManager * undoManager;
}
- (IBAction)changeColor:(id)sender;
- (IBAction)undo:(id)sender;
- (IBAction)redo:(id)sender;


@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.]
  kColor = kWhite;
  colorArr = [NSArray arrayWithObjects:[UIColor whiteColor], [UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor cyanColor], [UIColor blueColor], [UIColor magentaColor], nil];
  undoManager = [[NSUndoManager alloc] init];
  undoManager.levelsOfUndo = 7;

  NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
  [nc addObserver:self selector:@selector(undoManagerDidUndo:) name:NSUndoManagerDidUndoChangeNotification object:self.undoManager];
  [nc addObserver:self selector:@selector(undoManagerDidRedo:) name:NSUndoManagerDidRedoChangeNotification object:self.undoManager];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changeColor:(id)sender {
  [[undoManager prepareWithInvocationTarget:self]
   setcolor:kColor];

    kColor ++;
    if(kColor > kMagenta) kColor = kWhite;

  [self.view setBackgroundColor:colorArr[kColor]];
}
-(void) setcolor:(enum color) color{
  kColor = color;
  [self.view setBackgroundColor:colorArr[kColor]];
  NSLog(@"zzzz %d", color);
};

- (IBAction)undo:(id)sender {
  [undoManager undo];
}

- (IBAction)redo:(id)sender {
  [undoManager redo];
}

#pragma mark - Undo support

// -------------------------------------------------------------------------------
//  undoManagerDidUndo:
//  Handler for the NSUndoManagerDidUndoChangeNotification.  Redisplays the table
//  view to reflect the changed value.
//  See also: -setEditing:animated:
// -------------------------------------------------------------------------------
- (void)undoManagerDidUndo:(NSNotification *)notification
{
    //[self.view setBackgroundColor:colorArr[kColor]];
}

// -------------------------------------------------------------------------------
//  undoManagerDidRedo:
//  Handler for the NSUndoManagerDidRedoChangeNotification.  Redisplays the table
//  view to reflect the changed value.
//  See also: -setEditing:animated:
// -------------------------------------------------------------------------------
- (void)undoManagerDidRedo:(NSNotification *)notification
{
    //[self.view setBackgroundColor:colorArr[kColor]];
}
@end

1 个答案:

答案 0 :(得分:0)

//
//  ViewController.m
//  UndoManager
//
//  Created by Eugene Buleyko on 01.08.14.
//  Copyright (c) 2014 Eugene Buleyko. All rights reserved.
//

#import "ViewController.h"

enum color {kWhite = 0, kRed, kOrange, kYellow, kGreen, kCyan, kBlue, kMagenta};

@interface ViewController () {
    enum color kColor;
    NSArray * colorArr;
    NSUndoManager * undoManager;
}

- (IBAction)changeColor:(id)sender;
- (IBAction)undo:(id)sender;
- (IBAction)redo:(id)sender;

@end

@implementation ViewController

@synthesize buttonRedo, buttonUndo;

- (void)viewDidLoad
{
    [super viewDidLoad];

    kColor = kWhite;
    colorArr = [NSArray arrayWithObjects:[UIColor whiteColor], [UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor cyanColor], [UIColor blueColor], [UIColor magentaColor], nil];
    undoManager = [[NSUndoManager alloc] init];
    undoManager.levelsOfUndo = 9;

    NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(undoManagerDidUndo:) name:NSUndoManagerDidUndoChangeNotification object:self.undoManager];
    [nc addObserver:self selector:@selector(undoManagerDidRedo:) name:NSUndoManagerDidRedoChangeNotification object:self.undoManager];

    [self setcolor:kColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)changeColor:(id)sender {

    kColor ++;

    if (kColor > kMagenta) {
        kColor = kWhite;
    }
    [self setcolor:kColor];

    [self.view setBackgroundColor:colorArr[kColor]];

}
-(void) setcolor:(enum color) color{

//    if ([self.view backgroundColor] == colorArr[color]) {
//       NSLog(@"==");
//        
//    } else {
//        NSLog(@"!=");
//    }

    kColor = color;

    [[undoManager prepareWithInvocationTarget:self] setcolor:kColor];
    //[undoManager setActionname:@"Add"];
};


- (IBAction)undo:(id)sender {
    [undoManager undo];
}

- (IBAction)redo:(id)sender {
    [undoManager redo];
}

#pragma mark - Undo support

- (void)undoManagerDidUndo:(NSNotification *)notification
{
    [self.view setBackgroundColor:colorArr[kColor]];

    if (![undoManager canUndo]) self.buttonUndo.enabled = NO;
    if ([undoManager canRedo]) self.buttonRedo.enabled = YES;

}

- (void)undoManagerDidRedo:(NSNotification *)notification
{
    [self.view setBackgroundColor:colorArr[kColor]];

    if (![undoManager canRedo]) self.buttonRedo.enabled = NO;
    if ([undoManager canUndo]) self.buttonUndo.enabled = YES;
}


@end