美文网首页
NSUndoManager

NSUndoManager

作者: _健健 | 来源:发表于2016-05-24 16:35 被阅读65次

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.undoManager_ = [[NSUndoManager alloc] init];
    self.array = @[].mutableCopy;
    
    [_undoManager_ registerUndoWithTarget:self selector:@selector(addObjectMethod:) object:@"test"];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, 100, 100, 50);
    [btn setTitle:@"action" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void)addObjectMethod:(NSString *)str{
    [[self.undoManager_ prepareWithInvocationTarget:self] removeObjectMethod:str];//逆向删除
    [self.array addObject:str];
}

-(void)removeObjectMethod:(NSString *)str{
    [[self.undoManager_ prepareWithInvocationTarget:self] addObjectMethod:str];//逆向添加
    if ([self.array containsObject:str]) {
        [self.array removeObject:str];
    }
}

- (void)action{

    if(self.undoManager_.canUndo){
        [self.undoManager_ undo];
    }else if (self.undoManager_.canRedo){
        [self.undoManager_ redo];
    }
}

相关文章

网友评论

      本文标题:NSUndoManager

      本文链接:https://www.haomeiwen.com/subject/pcpzrttx.html