- (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];
}
}
网友评论