答案 0 :(得分:2)
答案 1 :(得分:0)
//这在具有两个不同数据源和撤消管理器的NSTableView子类上有效(在文档窗口中)
- (void)awakeFromNib
{
_undoManager1 = [NSUndoManager new];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(undoManagerNotification:) name:NSUndoManagerDidCloseUndoGroupNotification object:_undoManager1];
_undoManager2 = [NSUndoManager new];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(undoManagerNotification:) name:NSUndoManagerDidCloseUndoGroupNotification object:_undoManager2];
}
- (NSDocument *)document
{
return [NSDocumentController.sharedDocumentController documentForWindow:self.window];
}
- (void)undoManagerNotification:(NSNotification *)note
{
// NSUndoManagerDidCloseUndoGroupNotification: we made a change
[self.document updateChangeCount:NSChangeDone];
}
- (NSUndoManager *)undoManager
{
if ( self.window.firstResponder != self )
return self.window.firstResponder.undoManager;
// returns the right undo manager depending on current data source
return dataSource == dataSource1 ? _undoManager1 : _undoManager2;
}
- (IBAction)undo:(id)sender
{
[self.undoManager undo];
[self.document updateChangeCount:NSChangeUndone];
}
- (IBAction)redo:(id)sender
{
[self.undoManager redo];
[self.document updateChangeCount:NSChangeDone];
}
- (void)setValue:(id)newValue
{
[[self.undoManager prepareWithInvocationTarget:self] setValue:_myValue]; // NSUndoManager will post the NSUndoManagerDidCloseUndoGroupNotification
_myValue = newValue;
}
- (IBAction)doChangeSomeValue:(id)sender
{
[self setValue:someValue];
}